Drawing on a m-file via the command window; possible?

Hey, so I've created an m-file but there's far too many outputs that I don't need all the time for me to be happy with. However I'd like to be able to get their values without having to tweak the m-file and reruning it.
Example code (m-file);
a = 6 * 4
b = 1 * 9
Now that would obviously output to the command window;
a = 24
b = 9
However, let's say I have 100 equations like that. I naturally don't want them all outputting to the command window everytime I run the code so I'd suppress their outputs with semicolons.
However I might want to get the value of b from the command window, and having to unsupress that part of the code and rerunning is a bit of a flaf when the code may take a couple minutes to fully run.
I seem to recall having a simulink file that output to the command window and all I needed to do was go to the command window and type the output name to get that value in the command window (so in the above example I would simply type "a" into the command window to get the output "a = 24").
So I was wondering if there was something similiar that can be done in a m-file?
Sorry for the layout of this question by the way, struggling to try and explain what I wish to do! For the same reason it's been a nightmare trying to search for a solution!

2 Comments

Jan
Jan on 3 Jul 2012
Edited: Jan on 3 Jul 2012
The longer I think about it, the less I understand the problem. Tying "a" in the command window seems to be the best solution already, when you want something like "a = 24". If clear is a problem, do not call it. Or are you talking about the difference between M-scripts and M-functions?
I do not think that there is a magic dwim command (do what I mean), such that you simply have to program the wanted output manually.
The problem was (being a relative newcomer to MATLAB and having never really needed to use the workspace before) I didn't realise there was a difference between how M-scripts and M-functions stored to the workspace (i.e. I didn't realise that whilst M-scripts will store automatically, M-functions need extra code to manually store to the workspace).
This then resulted in my workspace not getting any variables stored. So when I would type say 'a', there wouldn't be a variable to look up.
Sorry for the confusion, and also for not knowing the difference between M-scripts and M-functions (I swear I'll get the hang of this MATLAB malarkey someday!).

Sign in to comment.

 Accepted Answer

You might want check this:
disp(a)
HTH,IH

3 Comments

disp(a) doesn't work if u hv typed clear, so how is it different from just typing a in the command window.
The poster asks,
"So I was wondering if there was something similiar that can be done in a m-file?"
There you go,
disp
If typing clear has disadvantages, do not type it.

Sign in to comment.

More Answers (1)

Yeah,you can do the similar think in a m-file as u did in your simulink file as long as you haven't typed clear in the command window.
Example:-
......
......
a = 6*4;
b = 1*9;
......
.......
Say the above was all in m-file and you run it and the variables are saved in workspace so you just need to type b in command window.

5 Comments

Variables will be saved in the base workspace only if they are created in m-script (not function).
If your m-file is a function file, you need to save the variables in base workspace manually using evalin() or assignin().
See now when I run the script the variables don't get saved to the workspace; thus when I type say "a" in the command window all I get back is "??? Undefined function or variable 'a'."
Fairly new to MATLAB so it's probably a really basic/silly mistake, but have no idea why it isn't exporting to the workspace :s
Edit: Scrap all that, just realised what I had been doing was just a bad habbit from when I first started using MATLAB. Think I have it sorted, shall have a fiddle to see if I definatly have it!
Edit 2: Just seen TAB's post which didn't show up until after I posted. Pretty much in a nutshell what I was doing wrong I think. When I first started using MATLAB we always created m-files as functions, not plain script.
Probably your m-file is a function file.
After creating variable 'a' in m-file save it to base workspace manually.
Example:
a=[10 20 30];
assignin('base','a',a);
After running your m-file, type
>> a
on command window.
That's brillaint, thanks a million!
Couple questions if that's ok?
1 - What does the base part do? I get the first a what you're naming the variable in the workspace and the second a is the value of the variable, but bit confused by the base part. Looked in the help file but still a bit confused. Are there multiple workspaces then with 'base' being the main one?
2 - If I wanted to save two (or more) variables to the workspace can I do so in one 'assignin'? For example if I had;
a = 4;
b = 7;
Instead of having to do;
assignin('base','a',a);
assignin('base','b',b);
Could I do something like;
assignin('base','a',a,'b',b)?
Thanks very much for the help, I do try and use the help files, etc. as much as possible but often get bogged down in the jargon. So I really appretiate people like yourself taking the time to help me learn the ropes!
1. Yes, there are multiple workspaces in matlab, like Base workspace, Function workspace and Global workspace. assignin() function saves the variable into the target workpsace.
See
>>doc assignin
2. Unfortunately assignin() can save one variable at time. You can collect all your variable into a structure and save it to workspace in just one function call.
For example:
% These are the variables
a = 4;
b = 7;
c = 100;
% Collect them in one structure variable
Mystruct.a = a;
Mystruct.b = b;
Mystruct.c = c;
% Now save the structure
assignin('base','Mystruct',Mystruct);
Now on command window you can see them using
>> Mystruct.a
>> Mystruct.b
>> Mystruct.c

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!