Organizing array of data skipping empty values
1 view (last 30 days)
Show older comments
Marco Gualtieri
on 7 Jul 2021
Commented: Marco Gualtieri
on 7 Jul 2021
Hello,
I need to organize data (now stored in .txt files) in either a single .txt file or in a struct; my problem is that this txt files, that are coming from simulations, miss a variable number of lines each. What I thought to do is to load each file inside a structure named P, and to create a struct file named M with each substructure having the maximum number of rows and columns that the files should contain, filled with zeros.
Then I wanted to write every row contained in the substructures of P in the corresponding substructures of M, leaving a row of zeros where the data files are missing a line. The .txt files all have in the first column an increasing value going from -5 to 25 with steps of 0.1, with the exception of the missing lines, as I said before. Can anyone help me on this part? How could I do?
Below you can see what I have written until now:
path='C:\Users\iberi\Desktop\Università\Magistrale\Tesi\MATLAB\Grafici_xx07\Polars\Section1\';
Re={'Re0100' 'Re0110' 'Re0120' 'Re0130' 'Re0140' 'Re0150' 'Re0160' 'Re0170' 'Re0180' 'Re0190' 'Re0200' 'Re0210' 'Re0220' 'Re0230' 'Re0240' 'Re0250' 'Re0260' 'Re0270' 'Re0280' 'Re0290' 'Re0300' 'Re0310' 'Re0320' 'Re0330' 'Re0340' 'Re0350'};
for i=1:length(Re)
M.(Re{i})=zeros(301,10);
P.(Re{i})=load([path, 'CLARK_Y_W=0.02_TEGap0.75_Blend0.6_FOIL2CAD_smooth_300_T1_' Re{i} '_M0.00_N9.0.txt']);
for j=1:301
for z=1:10
end
end
end
4 Comments
Yazan
on 7 Jul 2021
I'm not sure I understood what you are after. Do you need to read txt files in which some lines are empty?
Accepted Answer
Scott MacKenzie
on 7 Jul 2021
Edited: Scott MacKenzie
on 7 Jul 2021
I don't think you need nested for-loops. Use the 1st column of the data read as an index into the M.Rexxxx structure to direct the data into the corresponding rows in the M.Rexxxx stucture, leaving the remaining rows as zeros. Also, I don't see any reason to read the data into an intermediate structure. Just read the data into a matrix (e.g., P). Here's the general idea:
for i=1:length(Re)
M.(Re{i})=eye(301,10);
P=load([path, 'CLARK_Y_W=0.02_TEGap0.75_Blend0.6_FOIL2CAD_smooth_300_T1_' Re{i} '_M0.00_N9.0.txt']);
% move data from P into rows in M.Rexxxx structure, using 1st column in P as index
M.(Re{i})(P(:,1),:) = P;
end
5 Comments
Scott MacKenzie
on 7 Jul 2021
Well, if that worked, then OK.
BTW, there is one potential problem in rescaling as proposed. If the source data are missing either the expected min or max value (-5 or 25), then rescaling won't give you a clean and correct set of integer indices. The fix is to add a fake min and max to the source data before rescaling, then remove those rescaled values afterward:
idx = [-5; 25; P(:,1)];
idx = rescale(idx, 1, 301);
idx = idx(3:end);
M.(Re{i})(idx,:) = P;
Clearly, this wasn't an issue with your data. I guess every file has rows beginning with -5 and 25 and that the missing rows were somewhere between.
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!