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

3 views (last 30 days)
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.
Stephen
Stephen on 3 Jul 2012
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

Ilham Hardy
Ilham Hardy on 3 Jul 2012
You might want check this:
disp(a)
HTH,IH
  3 Comments
Ilham Hardy
Ilham Hardy on 3 Jul 2012
The poster asks,
"So I was wondering if there was something similiar that can be done in a m-file?"
There you go,
disp
Jan
Jan on 3 Jul 2012
If typing clear has disadvantages, do not type it.

Sign in to comment.

More Answers (1)

Luffy
Luffy on 3 Jul 2012
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
Stephen
Stephen on 3 Jul 2012
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!
TAB
TAB on 3 Jul 2012
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

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!