How to check the lenght of the characters in a file and how to save it using GUI
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kaavya N
el 11 de Jun. de 2021
I have read a .txt file in GUI , in another function I want to check if the file has less than 33 characters or not ,If it is less than 33 I want to save the file
function file_Callback(hObject, eventdata, handles)
[Filename,Pathname] = uigetfile('*.txt','Select Text File');
if isequal(Filename,0)
disp('User selected Cancel')
else
handles.f_id=fopen(Filename,'r');
handles.filename_txt=strcat(Pathname,Filename);
guidata(hObject,handles);
set(handles.text_path,'String',handles.filename_txt);
dr=dir(handles.filename_txt); size=num2str(dr.bytes);
filesize=strcat(size,' bytes');
set(handles.text_size,'String',filesize);
end
function Encode_Callback(hObject, eventdata, handles)
if length(text_path)<33
handles.textLabel = sprintf('Message to short');
set(textLabel, 'String', textLabel);
return;
end
end
How to pass the entire file and check its length in the Encode_Callback is less than 33
0 comentarios
Respuesta aceptada
Adam Danz
el 11 de Jun. de 2021
Editada: Adam Danz
el 11 de Jun. de 2021
Quite a bit of guess-work going on here regarding your GUI and what it's doing but, ... (see comments)
function file_Callback(hObject, eventdata, handles)
[Filename,Pathname] = uigetfile('*.txt','Select Text File');
if isequal(Filename,0)
disp('User selected Cancel')
else
handles.filename_txt = fullfile(Pathname, Filename); % use fullfile() to combine path and filename
set(handles.text_path,'String',handles.filename_txt);
fr = fileread(handles.filename_txt); % fileread is easier to implement and meets your needs
handles.filesize = numel(fr); % simply count characters (numeric output) not bytes
set(handles.text_size,'String',num2str(handles.filesize)); % Convert number -> char
guidata(hObject,handles); % Update handles at the end
end
function Encode_Callback(hObject, eventdata, handles)
if handles.filesize < 33 % no need for length()
handles.textLabel = sprintf('Message to short');
set(textLabel, 'String', textLabel); % ??????? no idea what this is/does
return;
end
end
end
7 comentarios
Adam Danz
el 12 de Jun. de 2021
Editada: Adam Danz
el 12 de Jun. de 2021
Ah, so you want to pass the file content. I suggest you pass the file path/name as shown in my previous comment and then read-in the file within the function.
My answer uses fileread to read the file and that returns one long character array of the entire file. You may need to read the file differently depending on how you plan to analyze it.
Más respuestas (0)
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!