Matlab Newbie here / I want to multiselect files but it always gives me an error

3 次查看(过去 30 天)
Here is my Code,if i multiselect them files it gives me this error,pls help
Error using fopen
First input must be a file name or a file identifier.
Error in m_Hommel_Rauheitsdaten_Auswertung_Einzelplot (line 18)
fid = fopen(filename,'r');
% Laden der Messdaten
[filename,filepath]=uigetfile({'*.prf','Result file (*.txt)'},'MultiSelect','On'); %Dateiauswahl ?ffnen und zugelassene Dateien festlegen
if isequal (filename,0) % Abbruch wenn keine Datei ausgew?hlt
disp('No file chosen, program is canceled.');
return;
end
v=(0:90:270)
Winkelposition=v;
Zeitpunkt=' Vorher ';
Lagernummer='Huelse';
Plotname=[Lagernummer Zeitpunkt Winkelposition];
%% Cell trfomation
fid = fopen(filename,'r');
i = 1;
tline = fgetl(fid);
C{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
C{i} = tline;
end
fclose(fid);
components = strsplit(C{4},' ');
myValue = components(5);
M=str2double(myValue);
skal=strsplit(C{7},' ');
myValeu= skal(3);
G=str2double(myValeu);
%%
Newfile=comma2point([filepath '' filename]);
opts = detectImportOptions(Newfile,'FileType','text');
Rauheitsdaten=readmatrix(Newfile,opts);
% Newfile=comma2point([filepath '' filename]);
% Rauheitsdaten=dlmread(Newfile,'\t',2,0);
% X=Rauheitsdaten(:,1);

回答(2 个)

Ameer Hamza
Ameer Hamza 2020-9-21
When MultiSelect is on, then uigetfile() returns a cell array. So you need to specify which file to open in fopen like this
fid = fopen(filename{1},'r'); % open first file
i = 1;
tline = fgetl(fid);
C{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
C{i} = tline;
end
fclose(fid);
Or write a for-loop to open all files one by one and read them.
  2 个评论
Firas Guechchati
Firas Guechchati 2020-9-21
thank you for this answer,but
it tells me Brace indexing is not supported for variables of this type.
Ameer Hamza
Ameer Hamza 2020-9-21
Yes, uigetfile seems to have strange behavior. If you select multiple files, it return a cell array, but if select return a single file, it returns a plain char array. You will need to use an if-block to take care of this condition
if iscell(filename)
fid = fopen(filename{1},'r');
else
fid = fopen(filename,'r');
end

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-9-21
Just make sure you include the folder because the user did not necessarily pick files in the current folder:
[filename, folder] = uigetfile({'*.*','Result file (*.txt)'},'MultiSelect','On'); %Dateiauswahl ?ffnen und zugelassene Dateien festlegen
if iscell(filename)
numFiles = length(filename);
else
if filename == 0 % User clicked cancel.
return;
end
% If we get here, the user clicked a single file.
numFiles = 1;
end
for k = 1 : numFiles
if iscell(filename)
fullFileName = fullfile(folder, filename{k});
else
fullFileName = fullfile(folder, filename);
end
fprintf('Opening %s...\n', fullFileName);
% fid = fopen(fullFileName,'r');
% etc.
end

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by