Set Size File Text on Matlab

Hi everyone, I have program that load text file using uigetfile :
[filepesan lokasi] = uigetfile({'*.txt'},'Browse file message');
this is capacity for file if text file is bigger than capacity
kapasitas=get(handles.txt_nkapaembed,'String');
kapa=str2num(kapasitas);
and codes for get text file size is
txtpesan = strcat(lokasi,filepesan);
infofile=dir(txtpesan);
file=infofile.bytes;
set(handles.txt_nukurfile,'String',num2str(file));
if (file >= kapa)
msg=strcat(num2str(kapa),{' bytes only will embed});
msgbox(msg,'warning','warn');
return
end
but in my algorithm program is that when text file is bigger than "kapasitas" size (ex. 100Kb), the text file should be "crop" until length of 100Kb.
how can I do that? need your advice, many thanks

2 Comments

You have not shown your code for reading the file.
Hi Mr. Walter, I put codes for reading the files, but now it will show if size file less than capacity, here is the code after return
set(handles.txt_lokasipesan, 'String', txtpesan);
bacapesan=textread(txtpesan,'%s','delimiter','\n','bufsize', 3000000);
set(handles.edt_pesanembedd,'String',bacapesan);

Sign in to comment.

 Accepted Answer

if (file >= kapa)
maxfilesize = 100*1024;
else
maxfilesize = inf;
end
set(handles.txt_lokasipesan, 'String', txtpesan);
fid = fopen(txtpesan, 'r');
buffer = fread(fid, maxfilesize, '*char');
fclose(fid);
bacapesan = regexp( buffer, '\n', 'split');
set(handles.edt_pesanembedd,'String',bacapesan);

10 Comments

Thanks Mr. Walter, but I put these codes and show error on command window
Error using ==> regexp
The first argument (STRING) must be a one-dimensional
array of char or cell arrays of strings.
Ah yes I keep forgetting that oddity of fread. Use this
buffer = fread(fid, [1 maxfilesize], '*char');
Many thanks Mr. Walter, but now, how can I save the "new text read" and load it into text box location?
set(handles.txt_lokasipesan, 'String', txtpesan);
for ex. first text file is ABC.txt, after process capacity save as DEF.txt then put in the same textbox, so it defined as new load text file, please need your advice
What rule should be used about building the new file name given the old file name?
And to check my understanding: you want to process a file and write out the new version of the file, and then you want to set the text box to be the name of the file you just created? In which case if the user asks to process a file then it will read in what was just written out and use that, so the output would be as if the original file was processed twice in a row ?
Yes, that what I mean Mr. Walter, output should be as the new text file and replace old text file in the same text box. because text file should be less or equal than capacity.
hi Mr. Walter, I've tried with these code, but it show only half of data
filemsg=get(handles.edt_pesanembedd,'String');
namemsg='plaintext.txt';
opened=fopen(namemsg,'w');
if opened>=0
fwrite(opened,namemsg{1});
fclose(opened);
end
txtfile='plaintext.txt';
set(handles.txt_lokasicipher,'String',txtfile);
but when I run these, it show only 600Byte from the real size is 13Kb
That code is not going to show anything for the text file, as you write to the file in that code, not read from it.
Is the file always a text file, or is it possible that it is a binary file?
With regards to the length, try
buffer = fread(fid, [1 maxfilesize], 'uint8=>char');
Hi Mr. Walter, yes, the file that I used is a text file. I tried to change the replacement text file codes with these, but it error
msgfile=get(handles.edit_msgfile,'String');
nama='plaintext.txt';
saving=fopen(nama,'wt');
if saving>=0
fwrite(saving,msgfile);
fclose(saving);
end
txtfile='plaintext.txt';
set(handles.txt_newtext,'String',txtfile);
the error command is
Error using ==> fwrite
Cannot write value: unsupported class cell
please help and need your advice Sir, many thanks
Your existing code,
bacapesan=textread(txtpesan,'%s','delimiter','\n','bufsize', 3000000);
resulted in bacapesan being a cell array of strings, so your code should already be expecting that. How did you handle it before now?
Hi Mr. Walter, finally I found the solution. but could you please check my last question about matlab compiler? it works when I run in m file, but not in exe compiler. Please help

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 3 Feb 2014

Commented:

on 4 Feb 2014

Community Treasure Hunt

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

Start Hunting!