Borrar filtros
Borrar filtros

Allowing for code to work different with file names

3 visualizaciones (últimos 30 días)
Aaron Smith
Aaron Smith el 25 de Mayo de 2017
Editada: Stephen23 el 5 de Jul. de 2017
I have a code that works fine but i have thus far only used it with a single test file. As a result of this i could run it while specifying the filename as well as the folder inside the code itself:
f1d = fopen(fullfile(sbd,'TEST_A.asc'),'rt');
Now that i need to apply it to multiple different files the code will not work. I have tried including a colon to read all files (as there is only one) but this doesn't work as the matlab error says there is no file name given. Can I not just specify the folder from which to select the file and the code read whatever files are in the folder?

Respuesta aceptada

Image Analyst
Image Analyst el 25 de Mayo de 2017
Aaron, if you just want the user to pick some file instead of using dir() or sprintf() to build/get the filename, do this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB'; % Whatever you want.....
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
  10 comentarios
Jan
Jan el 26 de Mayo de 2017
This line removed the entries '.' and '..' from the output of the dir() command. In your case they will not appear, because aou are searching for '*.asc'. But if you re-use the code snippets in other programs, this might be useful:
FileList = dir(fullfile(cd, '*.'));
fprintf('%s\n', FileList.name)
Then you see the '.' and '..', which are current folder and parent folder.
There is no general meaning of "status, msg". For the fopen and mkdir command, the first output is a flag to reply the success status and msg contains an error message in case of problem, such that the user do not have to guess, what the problem is. See:
doc fopen
doc mkdir
Aaron Smith
Aaron Smith el 26 de Mayo de 2017
Thanks a million :)

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 25 de Mayo de 2017
Editada: Stephen23 el 25 de Mayo de 2017
How to read multiple files is explained extensively in the documentation, on this forum, and in the wiki:
etc
The first thing to decide is if you want to generate the file names, or if you want to read the names of existing files:
  • generate names: use sprintf and fullfile.
  • read names: use dir and fullfile.
You can find some examples with my FEX submission natsortfiles:
  2 comentarios
Aaron Smith
Aaron Smith el 25 de Mayo de 2017
Editada: Aaron Smith el 25 de Mayo de 2017
Thanks Stephen. I am only processing a single file but i could use the code for importing multiple but is there no simpler way to simply select the folder and then matlab processes the file that is inside?. I am trying to read the file that exists inside the folder, not generate a file name
Stephen23
Stephen23 el 14 de Jun. de 2017
Editada: Stephen23 el 5 de Jul. de 2017
@Aaron Smith: and if you read my answer you will see that I covered that too:
"read names: use dir and fullfile."

Iniciar sesión para comentar.

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by