Importing single and multiple files

5 vues (au cours des 30 derniers jours)
Sandy Baha
Sandy Baha le 10 Fév 2016
Modifié(e) : Matt J le 10 Fév 2016
Hello, I want to import single as well as multiple files, by first code i am able to import multiple files but if i select single file it shows error, please help me to get the result. I have done some changes in code 2 but still error is there.
* * * * * * * * * * * * * * * |* *Item one**|***************
if true
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on')
for k = 1:length(fileList)
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName)
fprintf(1, 'Now reading %s\n', fullFileName)
end
  • * * * * * * * * * * * * * * * * * * * * * * * * * * * *Item two*****************************
if true
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on')
for k = 1:length(fileList)
if k==1
baseFileName = fileList
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n',fullFileName );
raw_data = importdata(fullFileName)
s = raw_data.data
else
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n',fullFileName );
raw_data = importdata(fullFileName)
s = raw_data.data
end
var = s(:,3)
s(:,3)= s(:,1)
s(:,1)= var
s = sortrows(s)
end

Réponse acceptée

Jan
Jan le 10 Fév 2016
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on');
fileList = cellstr(fileList);
for k = 1:length(fileList)
...
Now fileList is a cell string in all cases.
  2 commentaires
Sandy Baha
Sandy Baha le 10 Fév 2016
Thanks Jan and Ingrid for your valuable support
Sandy Baha
Sandy Baha le 10 Fév 2016
Modifié(e) : Sandy Baha le 10 Fév 2016
Jan, i have some text files which i will be importing and plotting the data. Data is in below format(shown in table below), i want to have plot between x and y keeping unique value same i.e.z data, it should display that unique value on graph for corresponding curve, means i want to plot between x axis = 100 200 300 and y =0.1 0.2 0.3 but there z value is 1 which will be as a text on plot. I have done something but not getting values for every curve, please help
if true
x y z a
100 0.1 1 65
100 0.4 2 71
100 0.7 3 66
200 0.2 1 55
200 0.5 2 68
200 0.9 3 70
300 0.3 1 72
300 0.6 2 77
300 1.0 3 67
end
if true
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on');
fileList = cellstr(fileList);
for k = 1:length(fileList)
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName)
fprintf(1, 'Now reading %s\n', fullFileName)
raw_data = importdata(fullFileName)
s = raw_data.data
var = s(:,3)
s(:,3)= s(:,1)
s(:,1)= var
s = sortrows(s)
E=unique(s(:,1))
pos=[];
for i=1:length(E)
k=find(s==E(i));
k=max(k)+i;
pos=[pos;k]
end
[r,c] = size(s);
add = numel(pos); % How much longer Anew is
Anew = NaN(r + add,c); % Preallocate
idx = setdiff(1:r+add,pos); % all positions of Anew except pos
Anew(idx,:) = s;
fclose('all')
x=Anew(:,1);
y=Anew(:,2);
z=Anew(:,3);
a=Anew(:,4);
hold on
figure(1)
plot(z,y,'*-','DisplayName',baseFileName);
hold all
for j = 1:length(E);
text(z(j),y(j),num2str(E(j)))
end
legend('-DynamicLegend','Location','southwest');
xlabel('x');
ylabel('y');
end

Connectez-vous pour commenter.

Plus de réponses (1)

Ingrid
Ingrid le 10 Fév 2016
is this what you try to achieve?
if true
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on')
if length(fileList) == 1
baseFileName = fileList
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n',fullFileName );
raw_data = importdata(fullFileName)
s = raw_data.data
else
for k = 1:length(fileList)
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n',fullFileName );
raw_data = importdata(fullFileName)
s{k} = raw_data.data
end
s = [s{:}];
end
var = s(:,3)
s(:,3)= s(:,1)
s(:,1)= var
s = sortrows(s)

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by