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

조회 수: 1 (최근 30일)
Stephen
Stephen 2012년 7월 3일
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
Jan
Jan 2012년 7월 3일
편집: Jan 2012년 7월 3일
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 2012년 7월 3일
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!).

댓글을 달려면 로그인하십시오.

채택된 답변

Ilham Hardy
Ilham Hardy 2012년 7월 3일
You might want check this:
disp(a)
HTH,IH
  댓글 수: 3
Ilham Hardy
Ilham Hardy 2012년 7월 3일
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 2012년 7월 3일
If typing clear has disadvantages, do not type it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Luffy
Luffy 2012년 7월 3일
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
Stephen
Stephen 2012년 7월 3일
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 2012년 7월 3일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by