How to paste text to function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
quynh tran
el 8 de Feb. de 2020
Comentada: Walter Roberson
el 10 de Feb. de 2020
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 comentarios
Stephen23
el 8 de Feb. de 2020
Editada: Stephen23
el 10 de Feb. de 2020
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.
Respuesta aceptada
Walter Roberson
el 8 de Feb. de 2020
Editada: Walter Roberson
el 8 de Feb. de 2020
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 comentarios
Walter Roberson
el 10 de Feb. de 2020
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Language Support en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!