Reading file from GUI edit text

4 vues (au cours des 30 derniers jours)
donga
donga le 6 Sep 2012
Commenté : Meryem le 22 Sep 2014
hi all,
I making gui that exams wav files, I want to write the file name in the gui edit text and to get the file details, the file is in my matlab dircetory
here is my code
% --- Executes during object creation, after setting all properties.
function file_input_CreateFcn(hObject, eventdata, handles)
% hObject handle to file_input (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in file_analyze.
function file_analyze_Callback(hObject, eventdata, handles)
% hObject handle to file_analyze (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%starting analyzing Function!!!!
name = str2double(get(handles.file_input,'String'));
[y, Fs] = wavread('name');
Y=fft(y,512);
m=Y.*conj(Y)/512;
f=Fs*(0:256)/512;
t=0:1/Fs:(length(y)-1)/Fs;
%Createing frequency Plot
axes(handles.frequency_axes)
plot(f,m(1:257))
i geting the error:Invalid Wave File. Reason: Cannot open file.

Réponse acceptée

Jan
Jan le 6 Sep 2012
Modifié(e) : Jan le 6 Sep 2012
name = str2double(get(handles.file_input,'String'));
[y, Fs] = wavread('name'); % ERROR!
This code looks for the file 'name.wav'. But even with this Matlab must fail:
[y, Fs] = wavread(name); % Another ERROR!
"name" has been defined as a double, such that it is not sufficient as a file name. I guess (and only guessing is possible due to the lack of information, hoiw the WAV file is called), that you need something like:
Folder = pwd; % Or better an exact defintion
number = str2double(get(handles.file_input,'String'));
FileName = fullfile(Folder, sprintf('File_%.3d.wav', number));
if exist(FileName, 'file') ~= 2
error('MyTools:', mfilename, ':ReadWAV'], 'Cannot find file: %s', FileName);
end
[y, Fs] = wavread(FileName);
  2 commentaires
Jan
Jan le 6 Sep 2012
donga has written:
hey, first thanks for the answer,
the file is locate in the root directory of the matlab. I know the files name, i just want to have the ability to analyzing any file that i am writing his name in the gui. is that possible to save the file not as double, as char for exmple?
*name = str2double(get(handles.file_input,'String'));*
thanks for any help
[Please post comments as comments, not as answer].
Jan
Jan le 6 Sep 2012
I do not understand, why you use doubles at all. What about this:
name = get(handles.file_input,'String');
Does "root directory of Matlab" mean something like "C:\Program files\Matlab\R2011b\"?! If so, don't do this. If "root directory" means the current directory as obtained by the cd command: Don't do it. Any subfunction, wild user or Timer-function can change the current folder. It is much safer to use absolute file names, and in consequence it saves hours of debugging.

Connectez-vous pour commenter.

Plus de réponses (1)

donga
donga le 10 Sep 2012
name = get(handles.file_input,'String');
work fine for me, thanks
  1 commentaire
Meryem
Meryem le 22 Sep 2014
Hi all,
I want to write the file name in the gui edit text and save edit text as my filename. Than I could be able to save filename as I wish. For instance; My file is an Neural network file which could be save as *.mat file; here is my code
%network_name is my edit text
name = get(handles.network_name,'string');
name = net;
save name
But it doesn't work I can't manage file name from edit text :(
It saves as name that l wrote next to save (name.mat). Thanks for your any answer...

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by