I wrote a few lines of codes to open a file containing a list of words, but it doesn't seem to open any file when I choose the file from the dialogue box. Can anybody tell what did I do wrong somewhere? I dont think the 'return' code was the problem because when i set it into comment, the error still existed.
[FILENAME, pathname] = uigetfile('*.wsb', 'rt');
if (isequal(FILENAME,0) || isequal(pathname,0))
disp('User pressed cancel');
else
disp(['User selected', fullfile(pathname, FILENAME)]);
end
fid = fopen(FILENAME,'rt');
if fid<0
%error could not find the file
return,
end

3 comentarios

sangeetha
sangeetha el 16 de Abr. de 2025
Movida: Voss el 16 de Abr. de 2025
S1=load(uigetfile('*.*','SELECT INPUT DATA'));
S=fft(S1);
sangeetha
sangeetha el 16 de Abr. de 2025
Movida: Voss el 16 de Abr. de 2025
error in this line
Walter Roberson
Walter Roberson el 16 de Abr. de 2025
Movida: Voss el 16 de Abr. de 2025
The first output from uigetfile is just the file name itself, without any directory information. If the file might not be in the current directory then you need something more like
[filename, pathname] = uigetfile('*.*','SELECT INPUT DATA');
S1 = load(fullfile(pathname, filename));
After that, the proper steps will depend upon the kind of file that was loaded. load() of an arbitrary file
  • might well fail, if the file is not one of a few very specific formats
  • might result in a numeric array
  • might result in a struct() with one field for each variable name loaded, if it was a .mat file
The cases that result in a numeric array are almost always better handled by readmatrix(). The cases that deal with .mat files need to reference the variable name that the data is stored in. The cases of a .mat file that has a single variable name stored in it can be handled with a small bit of code.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Nov. de 2011

0 votos

You need to fopen fullfile(pathname, FILENAME) rather than just FILENAME, except in the case where the chosen file just happens to be in the current directory.
I notice that you provide 'rt' as the second argument to uigetfile(). Does 'rt' happen to be what you want to prompt the user with? Not out of the question, of course, but that would be unusual: the 'rt' suggests confusion with the 'rt' argument that would need to be provided to the fopen() function [which you have done].

7 comentarios

NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi el 20 de Nov. de 2011
actually i just changed it to 'w+' and the file kind of runs. but it did not run through the loop that I have made on the next part of this code though. SO I am not sure if the file actually opened up or not..
Walter Roberson
Walter Roberson el 20 de Nov. de 2011
'w+' would be for writing in binary mode, and would erase any previous file contents. 'rt' would be for reading in text mode. Considering your "error could not find the file" comment, it sounds like you are planning to read rather than write ?
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi el 20 de Nov. de 2011
Editada: Walter Roberson el 16 de Abr. de 2025
I am confused about this line in uigetfile('.wsb','w+')
what would be the right thing to replaced the "w+" in this line of code?
[FILENAME, pathname] = uigetfile('*.wsb', 'w+');
if (isequal(FILENAME,0) || isequal(pathname,0))
disp('User pressed cancel');
else
disp(['User selected', fullfile(pathname, FILENAME)]);
end
fid = fopen(FILENAME,'w+');
if fid<0
%error could not find the file
return,
end
im not sure what do you mean by
"You need to fopen fullfile(pathname, FILENAME) rather than just FILENAME, except in the case where the chosen file just happens to be in the current directory."
how do correct my code then?I bluntly took the way of writing this code from the help window
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi el 20 de Nov. de 2011
It is more like reading the file, where the file would be a textfile containing thousands of words and then do some changes to it such as sorting the words in the file and delete some of the unnecessary words that might be duplicates etc..
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi el 20 de Nov. de 2011
If w+ is not correct, what should I use to correct the code then?
Fangjun Jiang
Fangjun Jiang el 20 de Nov. de 2011
uigetfile() just brings up a GUI for you to pick a file. Take a look at the help of uigetfile. [FILENAME, pathname] = uigetfile('*.wsb') is sufficient for you to choose from all the .wsb files. If you add the 'w+' as you did, the 'w+' would appear in the title bar in the GUI window that was brought up. It is meaningless and has nothing to do whether you want to write or read the file.
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi el 20 de Nov. de 2011
thank you, i think i got it now.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Nov. de 2011

Movida:

el 16 de Abr. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by