Importing single and multiple files

조회 수: 7 (최근 30일)
Sandy Baha
Sandy Baha 2016년 2월 10일
편집: Matt J 2016년 2월 10일
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

채택된 답변

Jan
Jan 2016년 2월 10일
[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
Sandy Baha
Sandy Baha 2016년 2월 10일
Thanks Jan and Ingrid for your valuable support
Sandy Baha
Sandy Baha 2016년 2월 10일
편집: Sandy Baha 2016년 2월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ingrid
Ingrid 2016년 2월 10일
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)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by