How to skip the first line from text file?
Mostra commenti meno recenti
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
dpb
il 7 Lug 2017
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.
Image Analyst
il 7 Lug 2017
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.
Walter Roberson
il 7 Lug 2017
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()
dpb
il 7 Lug 2017
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???
Walter Roberson
il 7 Lug 2017
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.
sivalogan satchithanandamoorthy
il 10 Lug 2017
Walter Roberson
il 10 Lug 2017
How are you reading in the text files? How are you reading in the csv files?
sivalogan satchithanandamoorthy
il 10 Lug 2017
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Data Import and Export in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!