How to paste text to function

2 views (last 30 days)
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
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.
quynh tran
quynh tran on 10 Feb 2020
@ 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
quynh tran
quynh tran on 10 Feb 2020
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
Walter Roberson
Walter Roberson on 10 Feb 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

Sign in to comment.

More Answers (0)

Categories

Find more on Language Support in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!