How to skip the first line from text file?

I have this multi-import file, which ask user to importchose text and csv file. My text file columns information on the first row and I want to skip the first line. Any help would be much appreciated.
Sincerely
siva
clear
clc
[fileName,filePath] = uigetfile({'*.csv';'*.txt'},'Pick a file','MultiSelect','on'); %filePath is were the folder address is stored
if isnumeric(fileName); %fileName is were the file address stored
disp('Please pick multiple file');
return;
end
pathToFile = fullfile(filePath,fileName); % getting file name and folder name
x= size(pathToFile,2); % size of the pathToFile
disp('Stress and strain column in csv format are as follows: 8 & 7');
disp('Stress and strain column in txt format are as follows: 4 & 5');
prompt ='Please choose the Stress column from csv format';
stress_column_csv =input(prompt);
prompt ='Please choose the strain column from csv format';
strain_column_csv=input(prompt);
prompt ='Please choose the Stress column from txt format';
stress_column_txt=input(prompt);
prompt ='Please choose the strain column from txt format';
strain_column_txt=input(prompt);
for i=1:x
[pathstr, name, ext{i}] = fileparts(pathToFile{i}); %had to add this line to get the file extension
store = importdata(pathToFile{i});
if ext{i} == '.csv'
[val,idx] = max(store.data(:,strain_column_csv)); % finding the max value
c ={idx}; % idx contain cell value.
stress{i}=store.data(1:c{1},stress_column_csv);
strain{i} =store.data(1:c{1},strain_column_csv);
else if ext{i}=='.txt'
% [val,idx] = max(store(:,stress_column_txt));
% c ={idx};
stress{i}=store(:,stress_column_txt);
strain{i} =store(:,strain_column_txt);
end
end
end
title_name=input('Title? ','s')
cc=jet(x);
for i=1:x
figure(1)
plot(strain{i},stress{i});
hold on
legendInfo{i}=[fileName{i}];
end
legend(legendInfo)
xlabel('strain [%]');
ylabel('Stress [MPa]');
grid on;
title(title_name);

8 Commenti

Probably be simpler if you showed a sample of the file(s) you're trying to read and what you want read. Not sure why there's any need for the discernment between .csv/.txt; seeing the file(s) would answer what the issues are.
importdata infers header rows automagically as well as delimiters or you can use the optional input parameters to give it some help in that regard.
Again, seeing the actual files would undoubtedly let someone simplify your script immensely.
Like dpb says, importdata() may well work. csvread() might also work if you have a later version of MATLAB. If you can't get either of those to work, then attach your data file.
One small thing to watch out for in the newest couple of releases is that importdata now defaults to importing text as string objects instead of as cell arrays of character vectors.
Another approach to consider is using readtable()
I just commented on importdata behavior as that was what OP is using but going through what seems like excessive gyrations to do so.
Alternatives are surely possible if we knew the file format, specifically, readtable certainly being a newer member of the family that has features to commend it.
OTOH, wonder how many scripts are broken by a change in return characteristics or is the string class really interchangeable with cellstr???
I have seen a couple of scripts broken because of the change -- but so far in each case it was because the scripts themselves already had bugs in how they handled the data.
I have multiple file. Some are text file while other are csv files. The problem is first line from text file contain heading and same thing with csv but matlab ignore first line from csv not from text. so how can i write a script, so it will automatically skip the first line from the text file using my script?
How are you reading in the text files? How are you reading in the csv files?
did not see your question until now. I do not get notification, if someone ask a question. I only get if for answer. Any way you would have found it by now, answer for the question above since you have answered my question.

Accedi per commentare.

 Risposta accettata

[pathstr, name, ext{i}] = fileparts(pathToFile{i}); %had to add this line to get the file extension
if strcmp(ext{i}, 'txt')
store = importdata(pathToFile{i}, ',', 1); %I am guessing about the delimiter
else
store = importdata(pathToFile{i});
end

6 Commenti

I have different script, which only contain text file only. so i used comment to skip the first line but, matlab store only the first line. Not sure what happening? I have delimiter as space. Any help please?
store = importdata(pathToFile{i}, ' ', 1);
When I test with a text file that has numeric data separated by spaces, importdata() automatically figures out that there is a header line.
Please post the first couple of lines of one of your files for testing. Also, which release are you using?
I am using 2017a. First couple of line
store = importdata(pathToFile{i})
alone should work on those files.
yea man it does, thanks

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by