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

3 views (last 30 days)
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);

Answers (2)

Ameer Hamza
Ameer Hamza on 21 Sep 2020
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 Comments
Firas Guechchati
Firas Guechchati on 21 Sep 2020
thank you for this answer,but
it tells me Brace indexing is not supported for variables of this type.
Ameer Hamza
Ameer Hamza on 21 Sep 2020
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

Sign in to comment.


Image Analyst
Image Analyst on 21 Sep 2020
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

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!