Hi everyone,
this is part of my code, I want to display LoadCase array (for example: HighWind).
LoadCase=input('Please specify the loading condition number (HighWind/HeavyIce/BrokenWire) ! ');
disp(LoadCase)
I don't know how;please help me.
Thanks.

댓글 수: 4

Geoff Hayes
Geoff Hayes 2014년 11월 15일
Hamid - what exactly are you expecting the user to enter when presented with the input request? A string or a number? Please provide an example, and describe what is going wrong with the above code. If you are observing errors (which you will for string inputs) then please post the error message.
hamid shakeri
hamid shakeri 2014년 11월 15일
Geoff - for example :
LoadCase=HighWind;
dis(LoadCase);
It doesn't get an error but display nothing, I want to display HighWind.
Thank you.
Geoff Hayes
Geoff Hayes 2014년 11월 15일
Hamid - are HighWind, HeavyIce, BrokenWire local variables in your code? Are they arrays, scalars, or something else? In the Command Window, just after running the above code, type
whos
to display all local variables, their sizes, etc.
hamid shakeri
hamid shakeri 2014년 11월 15일
size:1*1 , Bytes:8 , Class:double

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

 채택된 답변

Star Strider
Star Strider 2014년 11월 15일

0 개 추천

I would use the inputdlg function instead. It’s easier, and doesn’t clutter the Command Window:
LoadCase=inputdlg('Please specify the loading condition number (HighWind/HeavyIce/BrokenWire) ! ');
LoadCase = str2num(LoadCase{:})
This asks for and then displays your ‘LoadCase’ variable. Remember that ‘LoadCase’ is a cell, so if the response is supposed to be a number, you need to convert it to a double as I did here with the str2num function.
If the response is expected to be a string and not a number, use this to convert it to a string:
LoadCase = char(LoadCase)
Cell arrays are extremely useful, but in most instances it is necessary to convert them into another variable type to use their elements.

댓글 수: 8

hamid shakeri
hamid shakeri 2014년 11월 15일
Thank you for such a good idea but unfortunately it gets an error.
the above code is part of my code and the other part cause this error.
if LoadCase==HighWind || LoadCase==HeavyIce
SFv=1.5; SFw=1.65; SFt=2.5;
else
SFv=1; SFw=1; SFt=1;
end
??? Operands to the || and && operators must be convertible to logical scalar values.
Error in ==> if LoadCase==HighWind || LoadCase==HeavyIce
Thank you.
Star Strider
Star Strider 2014년 11월 15일
편집: Star Strider 2014년 11월 15일
My pleasure.
What variable types are: ‘LoadCase’, ‘HighWind’, ‘HeavyIce’ and any others of interest?
What do these commands return:
whos LoadCase
whos HighWind
hamid shakeri
hamid shakeri 2014년 11월 15일
size:1*1 , Bytes:8 , Class:double
Star Strider
Star Strider 2014년 11월 15일
Good. This should work then:
LoadCase = str2num(LoadCase{:})
And when I run this code:
LoadCase=inputdlg('Please specify the loading condition number (HighWind/HeavyIce/BrokenWire) ! ');
LoadCase = str2num(LoadCase{:})
HeavyIce = 2;
HighWind = 1;
if LoadCase==HighWind || LoadCase==HeavyIce
SFv=1.5; SFw=1.65; SFt=2.5;
else
SFv=1; SFw=1; SFt=1;
end
and enter a number for the input to the graphical user interface, it executed without error and gives the correct values for ‘SFv’ and the rest.
hamid shakeri
hamid shakeri 2014년 11월 15일
Thank you, I have no error more but my main question is I want to display HighWind at the the end of my code.
What I should do?
Thank you.
Star Strider
Star Strider 2014년 11월 15일
My pleasure!
Add this line, preferably shortly after the ‘inputdlg’ line:
LoadCond = {'HighWind' 'HeavyIce' 'BrokenWire'};
then near the end, add this line:
fprintf(1,'\n\tLoad Condition: %s\n', char(LoadCond(LoadCase)))
So if the user chooses option #1 ('HighWind'), the output will be:
Load Condition: HighWind
Change the fprintf call to whatever you want it to say.
hamid shakeri
hamid shakeri 2014년 11월 15일
You are the best, Thank you very much.
Star Strider
Star Strider 2014년 11월 15일
My pleasure! And thank you for the compliment!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Adding custom doc에 대해 자세히 알아보기

태그

질문:

2014년 11월 15일

댓글:

2014년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by