How to paste text to function
6 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I have this file mat.txt with cell array format:
year,month,day,hour,hst,doy,TempAvg,TempLow,TempHigh,HumAvg,HumLow,HumHigh,BaroAvg,BaroLow,BaroHigh,Windspeed,Gust,WindDirection,IntervalPrecip,SolarRadiationAvg,InsideTempAvg,InsideTempLow,InsideTempHigh,HeatIndex,WindChill,DewPoint,StationVoltage,wetbulb
I want to put the text above to the in the fucntion:
function textread(file)
[year,month,day,hour,hst,doy,TempAvg,TempLow,TempHigh,HumAvg,HumLow,HumHigh,BaroAvg,BaroLow,BaroHigh,Windspeed,Gust,WindDirection,IntervalPrecip,SolarRadiationAvg,InsideTempAvg,InsideTempLow,InsideTempHigh,HeatIndex,WindChill,DewPoint,StationVoltage,wetbulb] =...
textread(file,'%f %f %f %s %f %f %f %f %f %f %f %f %f %f %f %f %f','delimiter',',');
Can you help me to write code to copy text in txt file and paste it in the textread fuction?
I tried to use fprintf, textscan but it doesn't work with mix text like this
Thank you so much
4 件のコメント
Stephen23
2020 年 2 月 8 日
編集済み: Stephen23
2020 年 2 月 10 日
Naming your function textread is unlikely to work very well when inside that function you call a function named textread. Or is it your intention to write a recursive function with no stopping condition?
Nearly obsolete textread has this warning at the top of its documentation:
"I have alot of files with different variables so I don't want to take time to copy and paste it. This seems useless but I can save a lot of time and run it in program without opening the files"
This is entirely the wrong approach. You would be much better of using readtable or learning how to store/pass parameters in structures. The approach you have invented for yourself is an inefficient dead-end.
採用された回答
Walter Roberson
2020 年 2 月 8 日
編集済み: Walter Roberson
2020 年 2 月 8 日
S = fileread('mat.txt');
words = regexp(S, '\s*,\s*', 'split');
adjusted_words = matlab.lang.makeValidName, 'ReplacementStyle', 'delete');
varnames = string(matlab.lang.makeUniqueStrings(adjusted_words));
funname = 'my_textread';
fun_file = [funname, '.m'];
fid = fopen(fun_file, 'w');
fprintf(fid, 'function %s(file)\n', funname);
fprintf(fid, '[');
fprintf(fid, '%s,', varnames(1:end-1));
fprintf(fid, '%s] = ', varnames(end));
fprintf(fid, "textread(file, '%f %f %f %s %f %f %f %f %f %f %f %f %f %f %f %f %f','delimiter',',');\n");
fclose(fid);
clear(funname) %needed to get previous version out of working memory
You will now have my_textread.m containing a file that assigns the the variables whose name are contained in mat.txt, except cleaned up to remove spaces and to make sure that all of the names are unique.
For the purposes of this code you do not need to change your mat.txt file.
9 件のコメント
Walter Roberson
2020 年 2 月 10 日
That warning will not interrupt your program.
it means that there are some space in varaible names
Yes.
and the program can not read variable's name
Example for R2019b or later:
t = readtable('KaHonuaMomona.csv','PreserveVariableNames',true)
t.year %when the file variable is a valid MATLAB identifier
t.('Temp High') %when the file variable is not a valid MATLAB identifier
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Language Support についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!