Octave input from keyboard not working in a script - octave

I'm writing a script in octave that needs an input from keyboard in order to select a function to be used further. I've read the documentation and understood that
ans = input("choose...")
should wait for the for the input and store it in the ans variable. When I use it in the octave terminal it works just fine, but it doesn't when I insert it in my script : It shows the message "choose..." but it doesn't wait.
I don't get it, What I am missing.
Thanks,

Related

Unrecognized function or variable

I'm trying to run the beginning of a script that was sent to me:
function [ FList ] = ReadFileNames(DataFolder)
DirContents=dir(DataFolder);
FList=[];
DataFolder is the name of the folder in which all my data is held. When I click the Run button I receive:
ReadFileNames(DataFolder)
Unrecognized function or variable 'DataFolder'.
And I'm not sure why?
Any help is much appreciated. Thank you.
When you simply click Run, it's the same as running the command
ReadFileNames();
Note there are no inputs here, but your code expects (and requires) the input DataFolder.
So you have to do one of two things
Click the drop-down by the Run button, edit the type code to run prompt and add your folder. Then you can continue to click the Run button to execute this again. Note that once you've edited the default Run command for a function, the Run button icon changes slightly to reflect it:
A more common, and arguably more clear, way to run your function would just be to call it from the Command Window by typing ReadFileNames('your/folder/path/here'); (and hitting Enter to run).

Octave: View a figure

I have the following code saved as script.
% demonstration of hold
clf;
t = linspace (0, 2*pi, 100);
plot (t, sin (t));
hold on;
plot (t, cos (t));
title ({"hold on", "2 plots shown on same graph"});
hold off;
When I execute the script within Octave, Octave's viewer shows the figure.
However, when I execute the script from the command line (Ubuntu) the viewer opens and closes alone very quickly without showing any figure.
I don't know if this issue has to do with Octave or Ubuntu. I apologize if the question is very naive.
When running and Octave script from the command line, Octave is launched to execute it, and when the script ends, Octave terminates too. This is why you see the figure windows created and immediately destroyed. There no longer is a program running to show these figure windows.
If you add a pause statement at the end of your script, Octave will wait at that statement until you press a key, then continue. So after you press the key, the script ends and Octave terminates. But while it is waiting, the figure windows will be visible.
You can use waitfor to prevent Octave from termination until the figure is closed. First you should get graphic handle of the figure. Some functions including clf , plot ,... can return a graphic handle. Then use waitfor with the handle as its argument.
h = plot(1:10);
waitfor(h);
or
h = clf;
plot(1:10);
waitfor(h);

How do I run an R script from within RStudio's built-in R console?

I'm assuming it's like Python's import statement, but I'd like a quick answer, since I'm in the middle of an introduction class right now.
This was the closest I got, but it didn't seem to match the question, as it shows how to run an R Script from the system CLI, not the blue RStudio > prompt:
Run an R-script from command line and store results in subdirectory
Short Answer using source() function
Once you download, install, and open the RStudio, you'll see a part in the lower left with blue greater than symbols >.
In the part of RStudio's GUI with the blue >, enter the following
> setwd('/folder/where/the/file/is/')
> source('file_name')`
...output, if any, appears below...
Example:
Let's assume I have a file at /home/myusername/prj/r/learn_r/insurance_data.r that I want to run.
I would start up RStudio, and enter the following in the little window it has labeled Console:
Annoyingly long answer with screenshots using source() function
Well, it turned out to be much simpler than I expected to run this from RStudio's built-in console. I was surprised that this had not already been asked about RStudio, before. If it has, I guess I'll have a burned question.
Anyway, a little trial and error showed me how to do this:
Yay, output has appeared below.
Make sure to set your working directory, first.
I did this as follows from inside RStudio 1.0.143 on my Ubuntu 16.04 LTS environment:
setwd("~/proj/r/learn_r")
Next, you can enter help(source), you can search for the syntax of the source() function, and you can just type it in to the RStudio console for a prompt:
If you want to run a specific line from the R script, put the cursor somewhere in the line and press command+enter (on other pc I think is ctrl+enter). If you want to run the whole script or some parts, select the part and command+enter.

unable to call proc in TCL Invalid command name

I'm probably missing something obvious here but maybe you can help me.
I have a tcl script. Here it is in its entirety:
puts [encodeSDR 25]
proc encodeSDR {input} {
set sdr "test"
return $sdr
}
when I run this script I get an error:
c:\temp>tclsh testenviro.tcl
invalid command name "encodeSDR"
while executing
"encodeSDR"
invoke from within
"puts [encodeSDR 25]"
(file "testenviro.tcl" line 1)
What am I missing? Seems like this should work perfectly.
PS. Found possible reason:
There when I put the puts call below the proc it worked - so does it not load the entire script first? seems weird if that's the case but maybe thats it.
You are correct in that the entire script is not loaded before any execution takes place. A TCL script is interpreted one command at a time, and then will move to the next one only once it is finished. There is no compiling beforehand to check for procedures you may be referencing down below.
In your case, it first tried to interpret the puts command, which involved calling encodeSDR. Since that had not been defined in memory as of yet, the interpreter had no idea what you were trying to do.
One thing to watch out for is if you define the procedure by itself (say during testing/debug), and then later add it into a script in a way like your example. It will work just fine until you close out of the session, and TCL's memory gets released. The next time you load the script, however, it will fail because the procedure was never loaded for that session.

Calling functions from a spreadsheet formula

I'm starting to learn Google Apps Script and trying to complete "Your First Custom Function" tutorial. Maybe something has changed and they forgot to update the tutorial? When I type =in2mm(10) in a cell, it shows #NAME and Unknown function name popup is displayed.
So how do I call my own functions from a spreadsheet formula?
run your function just once through the editor's menu to be sure it is saved correctly then in the spreadsheet use it like this in cell B1 for example if A1 contains a numeric value :
=in2mm(A1)
or if you prefer, just like in your example : =in2mm(10) and it will show 254 ;-)
I solve it by deleting "inNum" in the function claiming part "function in2mm(inNum)", running it without any parameter in the spreadsheet once and getting THINKING... and ERROR, and retyping "inNum" in the function claiming part "function in2mm(inNum)", running and getting THINKING and correct result. Maybe it's the encoding issue when you copy and paste from the web page directly.