You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can I skip the first 3 lines and read two columns of numerical data as a matrix?
3 views (last 30 days)
Show older comments
Dear friends, Enclosed please find a text file. I want to skip the first 3 lines and read the remaining numerical data in 2 columns as a matrix. Many thanks in advance
Accepted Answer
Star Strider
on 14 Mar 2015
Use textscan:
fidi = fopen('rp_test.txt', 'rt');
d = textscan(fidi, '%f %f', 'HeaderLines',2);
figure(1)
plot(d{1}, d{2})
xlabel('"Flow Time"')
ylabel('"Area-Weighted Average Pressure Coefficient"')
grid
13 Comments
h
on 14 Mar 2015
Thank you again. Just one more question: If there are other files exactly in the same format but with the following names: rp_11 ,rp_12 ...., rp_18 , and rp_21 ,rp-22...., rp-28, in order to scan all the files, should I repeat that command for all of them? or can I write a loop to do that? (I have also installed a software which can change the name of the files in any format which may be helpful for developing a loop).
Star Strider
on 14 Mar 2015
You can use a loop. To do ‘rp_11’ and the others, I would do something like this:
n = 11:28;
for k1 = 1:length(n)
filename = sprintf('rp_%02d.txt', n(k1));
fidi = fopen(filename, 'rt');
d{k1} = textscan(fidi, '%f %f', 'HeaderLines',2);
fclose(fidi);
end
It saves each file as an element of cell array ‘d’. To read or plot all of them, the plot loop would be:
for k1 = 1:length(n)
figure(k1)
plot(d{k1,1}, plot{k1,2})
xlabel('"Flow Time"')
ylabel('"Area-Weighted Average Pressure Coefficient"')
title(sprintf('rp_%02d', n(k1))
grid
end
I can’t test this because I don’t have all your files. The plot should work as written, but if it doesn’t, reverse the order of the indices in the plot call.
And Happy π Day (3-14-15)!
h
on 15 Mar 2015
Thank you very much for your really kind help. I have tried to run your code in order to save the output of each file in separated matrices, but had some troubles with it. I have enclosed all of my files to this massage. There are two types of name series. I am wondering if you could take a look at them, and give me some insight. Happy π Day (3-14-15) to you too...
Stephen23
on 15 Mar 2015
@h: "I have tried to run your code in order to save the output of each file in separated matrices": what code did you try running, what did it do that you did not expect, and what messages or outputs did you get?
Are you trying to create dynamically named variables? Star Strider's solution saves the matrices in a cell array, and this is the best way to import them. Do not create individual dynamically named variables in the loop, this is bad programming practice.
Star Strider
on 15 Mar 2015
My pleasure!
This was actually fun, although it took a bit longer than I’d expected to complete it. (We’re GMT-6 here and it’s 22.55 MDT, so I’m not at my best.)
With that gentle caution that I may have overlooked something, this seems to work with both sets of files and produces nice plots. I won’t explain it in full, but there’s nothing really sophisticated about it. It’s fairly straightforward. It reads the files and plots the data in two separate loops.
The commented-out lines are specific for the various file types, so un-comment the ones you need for one set and do the reverse for the other set.
The ‘h_test_files’ variable is the full path to the ‘files’ directory. I couldn’t get my usual trick to have MATLAB see that directory, and being too tired to troubleshoot it, simply brute-forced it. (It worked.) You will have to change it in your system, but then you may not need to use it since the files are already on your machine.
You may have to tweak the file reading loop a bit so it will produce the correct ‘filename’ (directory\file), but it should work without alteration otherwise.
The code:
h_text_files = 'C:\Users\USERNAME\Documents\MATLAB\USER_Files\R & D\Answers & CSSM Problems\Temporary Projects\files\';
k1v = [];
nrp = 11:88;
npinlet = 1:26;
for k1 = 1:length(nrp)
% filename = [h_text_files sprintf('rp %02d.txt', nrp(k1))]; % For ‘rp’ Series
% filename = [h_text_files sprintf('rp_%02d.txt', npinlet(k1))]; % For ‘p_inlet’ Series
if exist(filename, 'file')
fidi = fopen(filename, 'rt');
d = textscan(fidi, '%f %f', 'HeaderLines',2);
fclose(fidi);
end
if ~isempty(d)
dv{k1} = d;
k1v = [k1v, k1];
end
end
for k1 = 1:length(kiv)
figure(k1)
dp = cell2mat(dv{k1});
plot(dp(:,1), dp(:,2))
xlabel('"Flow Time"')
% ylabel('"Area-Weighted Average Pressure Coefficient"') % For ‘rp’ Series
% ylabel('"Area-Weighted Average X Velocity"') % For ‘p_inlet’ Series
% title(sprintf('rp %02d', nrp(k1v))) % For ‘rp’ Series
% title(sprintf('p inlet%02d', npinlet(k1v))) % For ‘p_inlet’ Series
grid
end
It looks a bit kludgy in places, but it works. I tried it with all your files. BTW, I put in the ‘exist’ file trap and ‘isempty’ variable trap because some files that didn’t exist come up empty, and I didn’t design the logic to break out of the loop in that instance. (Blame fatigue.)
It works. Feel free to tweak it to make it more efficient.
h
on 15 Mar 2015
I really appreciate your efforts and kind attention. I have adjusted the code in the way that you described, but I am consistently getting the following error: ??? Undefined function or variable 'd'.
Error in ==> Untitled at 13 if ~isempty(d) . I have no idea why it is not working...
Star Strider
on 15 Mar 2015
I should never code late at night!
The correct (and efficient) loop:
kv = [];
k2 = 0;
nrp = 11:88; % For ‘p_inlet’ Series
npinlet = 1:26; % For ‘rp’ Series
for k1 = 1:length(npinlet) % <—< Change To Appropriate Vector
% filename = [h_text_files sprintf('rp_%02d.txt', nrp(k1))]; % For ‘rp’ Series
filename = [h_text_files sprintf('p_inlet_%02d.txt', npinlet(k1))]; % For ‘p_inlet’ Series
if exist(filename, 'file')
fidi = fopen(filename, 'rt');
k2 = k2 + 1;
d{k2} = textscan(fidi, '%f %f', 'HeaderLines',2);
fclose(fidi);
kv = [kv, k1];
end
end
for k1 = 1:length(kv)
figure(k1)
dp = cell2mat(d{k1});
plot(dp(:,1), dp(:,2))
xlabel('"Flow Time"')
% ylabel('"Area-Weighted Average Pressure Coefficient"') % For ‘rp’ Series
ylabel('"Area-Weighted Average X Velocity"') % For ‘p_inlet’ Series
% title(sprintf('rp %02d', nrp(kv(k1)))) % For ‘rp’ Series
title(sprintf('p inlet %02d', npinlet(kv(k1)))) % For ‘p_inlet’ Series
grid
end
The ‘h_text_files’ assignment is unchanged.
If you only intend to run this once for each data set, then at the end of each run for the ‘rp’ series and separately for the ‘p_inlet’ series, use the save function to save all of the calculated ‘d’ and ‘kv’ variables as .mat files.
In my code, the save call for the ‘p_inlet’ series is:
save([h_text_files 'P_INLET.mat'], 'd', 'kv');
You can then just load them as you need them. When you do that, since they will all be named ‘d’ internally in the .mat files, make them unique by perhaps doing something like (in my code):
p_inlet = load([h_text_files 'P_INLET.mat']); % Load ‘p_inlet’ Variables
then the first set for ‘p_inlet’ would be:
p_i(1) = p_inlet.d(1); % Define First ‘d’ Set
check = p_i{1} % Display Contents (Info Only)
See the information on structures to understand how to address the output of the load call, since it returns a structure. (This lends itself to a loop if you want to do that.) You may have to experiment with this, but it will work!
h
on 16 Mar 2015
Edited: h
on 16 Mar 2015
My friend, I am really sorry for my time-consuming problem. I am still not getting the results, and do not know how to run it properly... I just changed the following line: filename = ['C:\Users\HCoM\Desktop\files' sprintf('p_inlet_%02d.txt', npinlet(k1))]; and commented the lines related to 'rp' series. But it is not working (I mean I cannot see what you explained in .mat file). Have you examined the code with my files? Is it working well on your system? I am using MATLAB R2010b; maybe there are some incompatibility between my version and yours.
Star Strider
on 16 Mar 2015
My latest code worked perfectly with your example files. I tested it with all of them. I also tested the .mat-file save and load calls.
You may need to put a backslant (\) in your ‘filename’ assignment to make the directory work as a path in Windows:
filename = ['C:\Users\HCoM\Desktop\files\' sprintf('p_inlet_%02d.txt', npinlet(k1))];
Try that line and see if it solves the problem.
h
on 16 Mar 2015
YES, FINALLY it works... You are right... I just needed to put that (\). I have been so excited once I saw the figures... Many thanks for your persistent help... you did a GREAT job for me, and saved my time a lot, as I am using a program which produces those input files after each run...
More Answers (1)
Image Analyst
on 14 Mar 2015
You didn't attach anything but I'd recommend trying dlmread(). It has an input option where you can tell it to skip past some specified number of header lines.
3 Comments
h
on 14 Mar 2015
Hi, Thank you for your reply, and sorry for no attached file. I have modified my post. Actually, I am a beginner in Matlab and not familiar with this command. I previously simply erased the first lines and used "load" command for similar cases. but in my current case, I have many files to read in Matlab, and it takes too much time if I want to go thorough the files manually. Will be grateful if you could help me more in detail...
Image Analyst
on 14 Mar 2015
Edited: Image Analyst
on 14 Mar 2015
You could have looked up dlmread() or importdata(), but it looks like Star gave you a solution that also works.
A = importdata(fullFileName, delimiterIn, headerlinesIn);
To read many files, see the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F If our suggestions help, then you can "Vote" for our suggestions.
See Also
Categories
Find more on Data Import and Analysis 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)