简体   繁体   中英

automating string and number concatenation

I'm having some trouble automating a matlab script which should prompt the user for the variable they are interested in, as well as the date range they want. I then want the script to concatenate their answers within a naming convention for the file they will ultimately load.

variable=input('please input variable of interest');

% temp

start=input('please state the start date in the form yymmdd: ');

%130418

enddate=input('please state the end date in the form yymmdd: ');

%140418

file=sprintf('%s_dailydata_%d_%d.csv',variable,start,enddate);

%so I thought 'file' would look like: temp_dailydata_130418_140418.csv

vardata=load(file);

the two numbers representing the dates are not causing any issues, but the fact that 'variable' is a string is. I know if I put apostrophes before and after 'temp' when I'm promted, it will work, but I have to assume that the end user won't know to do this. I tried putting curly braces around 'please input your variable..', but that didn't help either. Obviously this approach assumes that the date being requested exists in a filename. Can anyone offer any advice? Perhaps the sprintf function is not the best option here?

Don't use 'end' as a variable name, it is a reserved name and using it could create conflicts with any function or logic block you're defining.

If you know your input is going to be a string: from the documentation for input()

str = input(prompt,'s') 
Returns the entered text as a MATLAB string, without evaluating expressions.

As for knowing whether or not the file exists, that's something you'd have to incorporate some error logic for. Either a try/catch block with your load() call or you could use uigetfile() to get the filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM