Importing single and multiple files

3 次查看(过去 30 天)
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 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by