Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How to read a file, using variables and * for the file name?

1 Ansicht (letzte 30 Tage)
Pedro Augusto de Castro e Castro
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Hello,
I want to read some files, but I want to find them using variables and also *. That's because I have variables for deltaI, Fsw and material, but not for L. Here is what I tried so far, without much succes.
deltaI = handles.currentPointResults(k).Ripple(found_fsw).DeltaI;
Fsw = handles.currentPointResults(k).Ripple(found_fsw).Fsw / 1000;
material = handles.currentPointResults(k).Ripple(found_fsw).Material;
try
returnValue = uigetdir(handles.Nucleo_folder,'Selecione a pasta com os relatórios dos indutores');
% returnValue will be 0 (a double) if they click cancel.
% returnValue will be the path (a string) if they clicked OK.
if returnValue ~= 0
% Assign the value if they didn't click cancel.
handles.Nucleo_folder = returnValue;
% set(handles.txtFolder, 'string' ,handles.T_Folder);
guidata(hObject, handles);
% Save the image folder in our ini file.
% SaveUserSettings(handles);
end
catch ME
% Some error happened if you get here.
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s', ...
mfilename, callStackString, ME.message);
WarnUser(errorMessage);
end
filename = "\Inverter_DeltaI=" + num2str(deltaI) + "_Fsw=" + num2str(Fsw) + "kHz_L=*uH_Material=" + material + ".txt";
found_file = fullfile(handles.Nucleo_folder,convertStringsToChars(filename));
fid = fopen(found_file)
If I replace * for a specific number (from an existing file), this code works. Do you guys know how to do this?
  2 Kommentare
Stephen23
Stephen23 am 21 Mai 2020
fopen requires an exact literal filename, it does not allow any wildcard characters.
If you want to use wildcard character * you will need to use dir to get a list of the matching files.

Antworten (1)

Image Analyst
Image Analyst am 21 Mai 2020
Try this:
try
returnValue = uigetdir(handles.Nucleo_folder,'Selecione a pasta com os relatórios dos indutores');
% returnValue will be 0 (a double) if they click cancel.
% returnValue will be the path (a string) if they clicked OK.
if returnValue ~= 0
% Assign the value if they didn't click cancel.
handles.Nucleo_folder = returnValue;
% guidata(hObject, handles);
end
filePattern = sprintf('\Inverter_DeltaI=%s_Fsw=%f kHz_L=*uH_Material=%s.txt', deltaI, Fsw, material)
%filePattern = '*.*';
fileList = dir(filePattern)
numberOfFiles = length(fileList);
for k = 1 : numberOfFiles
fullFileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Processing file #%d of %d : %s\n', k, numberOfFiles, fullFileName);
fid = fopen(found_file, 'rt')
if fid == -1
continue; % Can't open. Skip this file.
end
% Process the file somehow.....
end
catch ME
% Some error happened if you get here.
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s', ...
mfilename, callStackString, ME.message);
WarnUser(errorMessage);
end

Community Treasure Hunt

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

Start Hunting!

Translated by