See workspace in MATLAB command line terminal - matlab

Is there any command to see all variable names, types and values in command line interface? Similar to Matlab's Workspace? I already know about command whos but it doesn't show the values, It just shows names and types.
Thanks :)

try this:
vals = who;
for val = vals
eval(val.name)
end

Related

Issue with nume1 in MATLAB

Here's a copy-paste of what MATLAB is doing after I type nume1(A) into the command window:
nume1(A)
%Undefined function 'nume1' for input arguments of type 'double'.
Did you mean:
numel(A)
ans =
1034289
Does anyone know how to stop matlab from doing this? This is preventing me from using nume1 in the editor...
Let's assume this isn't a typo and you didn't mean to invoke the numel function.
Make sure you're not trying to call an M-script as a function. The first line in your nume1.m file should read something like
function argout = nume1(argin)
You should make sure your function is either on your MATLAB search path (type path in the Command Window to see what MATLAB's current search path is), or in your current working directory (type pwd).
To see which function or script MATLAB will use, if any, type:
>> which nume1 -all
The top most is the one that will be invoked.

Matlab Error: "Undefined function or method X for input arguments of type 'double'" With Recursion

I'm trying to make Sierpinski triangles with recursion but I get this error:
??? Undefined function or method 'sierpinski' for input arguments of type 'double'.
I understand that it has to do with Matlab not finding the path for my function, but the weird thing is that it can find my main sierpinski(x,y,n)-function but not the same function that I'm trying to call later in order to get recursion.
My code looks something like this:
function sierpinski(x,y,n)
...
sierpinski(x2,y2,n-1)
end
sierpinski([0,1,0.5],[0,0,1],4)
I would be very grateful if someone could help me with this :)
I cannot reproduce the first error you report. It probably has to do with the file not being on the path. The easiest way to avoid this is to change the working directory to the directory that contains the .m file.
The second error you describe in your comment is due to the fact that you're trying to have a file that is a Matlab function and a Matlab script at the same time. Both have the extension .m, but the first contains a function definition (something that can be called with arguments, has local variables, and can return values), and the other one contains a series of matlab statements that are to be executed exactly as if they were entered one by one in the command window.
Do the following:
– Make a Matlab function file sierpinski.m which includes only your function code:
function sierpinski(x,y,n)
hold on
if n == 0
fill(x,y,'r')
else
x2 = [(x(2)-x(1))/2, (x(2)-x(3))/2, x(3)+(x(2)-x(3))/2];
y2 = [y(1), y(3)/2, y(3)/2];
sierpinski(x2, y2, n-1)
end
Save the file to the current directory or a directory on the path.
– In the command window, enter the statement sierpinski([0,1,0.5],[0,0,1],2). The result is a figure window with a skewed red triangle. Not a Sierpinski triangle, but I guess the first step is done. ;-)
Instead of entering that statement in the command window, you can also make a Matlab script file. Edit a file with the name e.g. run_sierpinski.m, which contains the statement:
sierpinski([0,1,0.5],[0,0,1],2)
Again, save the file to the current directory or a directory on the path.
Now you can run the script, either by clicking the "Run" button in the GUI (green triangle or so), or by entering run_sierpinski in the command window. Either way, the result should be the same as entering the statement directly.

Matlab fopen fails to open file name passed as string variable but opens when passed as string

Hi all I'm trying to read in files using a variable and for some reason when I pass the same string as a variable it no longer opens. Below I try the same command simply swapping the variable for its contents and get different results? I also tried DEBLANK and STRTRIM from checking other questions.
f=fopen(fname,'r');
f
f =
-1
fname
fname =
/xchip/cga_home/amaro/Cranios/Segs/001-CN-001-CN-N.tsv
f=fopen('/xchip/cga_home/amaro/Cranios/Segs/001-CN-001-CN-N.tsv','r');
f
f=3
Hey sorry the example wasn't helpful. I solved this problem by reverting to an older version of matlab from 2013a to 2012b. Basically matlab 2013a was treating the file handle differently when passed as a variable.

Using matlabs save in functions

Is it possible to use the Matlab save command inside a function to store workspace variables?
Consider following scenario: I've got a bunch of variables in the Matlab workspace and want all that are beginning with "a" and "b" in a .mat file. Of course this works:
save('test.mat','a*','b*')
but i want to have a variable filename. The function i wrote:
function save_with_name(name)
save(name,'a*','b*')
does not work, because save_with_name doesn't see the workspace variables. Is there a solution which i can use?
You need to evaluate save in the base workspace.
function save_with_name(name)
expression = ['save(''', name, ''',''a*'',''b*'')'];
evalin('base',expression);
The double-quotes ('') in the expression are necessary to allow the quote character itself (').
Thus the command you're looking for is: evalin

Matlab: How to know the name of the file are you using in the workspace?

sometimes I load a .m file to work in the workspace, but I usually forget what is the recent file I´ve opened. So in the same way that you write who and you can see the variables of the workspace I suppose there should be a command to know what is the .m file in which you are working.
Does anyone know a command like this?
Thank you so much.
For newer MATLAB versions, there is a way to get the fullpath of the file currently being edited in the Editor:
if verLessThan('matlab', '7.10')
%# not supported
fname = '';
elseif verLessThan('matlab', '7.12')
%# R2010a,R2010b: editorservices
fname = editorservices.getActiveFilename;
else
%# R2011a: matlab.desktop.editor API
fname = matlab.desktop.editor.getActiveFilename;
end
mfilename seemed to be the right choice, but it returns an empty string when called from the command line. Therefore you may check the 'command history' provided by the IDE.
For larger projects it most often makes sense to use MATLAB's object model or at least functions in order to structure your work. Working in the 'workspace' often results in unwanted side effects.
I do something like this to bootstrap runs:
%%
params.run = 'hairy.m';
params.hair = 10;
setup_defaults(params);
run_lots_of_code(params);
This has advantages as it hides the 'globals' in a single global (params), and the global tells you where they came from.