简体   繁体   English

自动字符串和数字串联

[英]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. 我在自动化matlab脚本时遇到了一些麻烦,该脚本会提示用户输入他们感兴趣的变量以及他们想要的日期范围。 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'); variable = input('请输入感兴趣的输入变量');

% temp 温度百分比

start=input('please state the start date in the form yymmdd: '); start = input('请以yymmdd:'的形式声明开始日期;

%130418 %130418

enddate=input('please state the end date in the form yymmdd: '); enddate = input('请以yymmdd:'的形式声明结束日期;

%140418 %140418

file=sprintf('%s_dailydata_%d_%d.csv',variable,start,enddate); 文件= sprintf的( '%s_dailydata_%D_%d.csv',变量,启动,结束日期);

%so I thought 'file' would look like: temp_dailydata_130418_140418.csv 所以我认为“文件”看起来像是:temp_dailydata_130418_140418.csv

vardata=load(file); VARDATA =负载(文件);

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. 我知道如果在提示时在'temp'之前和之后放置撇号,它将起作用,但是我必须假设最终用户不知道这样做。 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? 也许sprintf函数不是这里的最佳选择?

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. 不要将“ end”用作变量名,它是保留名,使用它可能会与您定义的任何功能或逻辑块产生冲突。

If you know your input is going to be a string: from the documentation for input() 如果您知道您的输入将是字符串,请参见: 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. 可以通过load()调用try / catch块,也可以使用uigetfile()来获取文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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