How to paste text to function

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 Comments

Temp Avg is not a valid variable name.
If yu did manage to inject those variables into your function, then you would have the difficulty that the values would be stuck inside the function, since you do not return anything.
@Walter Roberson: thank you, you are right. I changed the varaible in the post. Will try find the way to change them in text file
Stephen23
Stephen23 on 8 Feb 2020
Edited: Stephen23 on 10 Feb 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:
"textread is not recommended. Use textscan instead."
"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.
@ Stephen Cobeldick Thank you for following my question and give advices. Actually, my function name is not textread. I just put it in the file to not distract people to others problems. But I think you two are right about using readtable instead of textread. I will read the whole function again and see if I can make it easier. Thanks.

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 8 Feb 2020
Edited: Walter Roberson on 8 Feb 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 Comments

Thank you so much for your support. Here is the final scripts in case someone needs:
S = fileread('mat.txt');
words = regexp(S, '\s*,\s*', 'split');
adjusted_words = matlab.lang.makeValidName(words,'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);
funname should not have the space before the second '
Good point though about the need to double the %. There are other ways that could have been handled though.
It is still a useless function however, as it assigns to the variables only in the context of the function, and then they are immediately thrown away.
Also the fileread should not have a space before the second '
Ouch, that hurts my eyes: meta-programming with dynamic variable names.
Difficult to write, difficult to check, difficult to debug, difficult to maintain, complex, inefficient, ...
quynh tran
quynh tran on 10 Feb 2020
Edited: quynh tran on 10 Feb 2020
@Walter Roberson: Thanks for reminding, I fixed it. 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 :).
But all the function does is read the data and then discard the variables. There is no point in running the function if you do not return the values. And if you return the values, then inside the function it does not matter what variable names you use so you do not need dynamic programming.
I would suggest to you that readtable() with 'VariableNames' option would be a better choice, especially if you are using R2019b or later (in which case you do not even need to change the variable names from the file -- not unless there happens to be a duplicate in the file.)
My software is only compatible with Matlab2017. But even using Matlab2019, I got this error while using readtable:
t = readtable('KaHonuaMomona.csv')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property. Set 'PreserveVariableNames' to true to use the original column headers as table variable names.
This warning interupted my program.
If I change the code like:
t = readtable('KaHonuaMomona.csv','PreserveVariableNames',true)
it means that there are some space in varaible names and the program can not read variable's name . That's why I have to use textread
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

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!