I'm currently working in MatLab AppDesigner. I am trying to get MatLab to download the contents of an editable Text Area as a specific type of file (.cfile) and save this file to a specific location. The user should be able to edit the code of the file in the Text Area, and then click a button which will cue MatLab to download the file as a .cfile and save it to a specific location on the user's device. Initially, the Text Area will contain a default code setup, which the user can modify before downloading. I am relatively new to MatLab; does anyone know how I could do this? Thanks!

10 commentaires

Rik
Rik le 2 Août 2018
Download means it is loaded from a web page of some sort, but the rest of your description sounds like it's just saving a file.
Hanna Al-Kowsi
Hanna Al-Kowsi le 2 Août 2018
Yeah essentially I just want the user to be able to save the contents of the Text Area as a specific file type.
Rik
Rik le 2 Août 2018
Then I would create a figure with a text field and a save button. The callback function for that uicontrol button would then use get to load the contents of the text field to a cell array. You can use uiputfile to ask te user for a file location. Then the writing of the file depends on your file format.
Hanna Al-Kowsi
Hanna Al-Kowsi le 2 Août 2018
I'm sorry, I'm really new to MatLab, could you type out some of the code for me?
Hanna Al-Kowsi
Hanna Al-Kowsi le 2 Août 2018
I have the text file and the save button created, but I'm not sure how to correctly format the code to download the file and save it.
Rik
Rik le 2 Août 2018
I mentioned several functions. Did you read the corresponding documentation pages?
Hanna Al-Kowsi
Hanna Al-Kowsi le 3 Août 2018
Modifié(e) : Hanna Al-Kowsi le 3 Août 2018
I read through the documentation pages for each function, and this is my code right now:
function saveButtonPushed(app,event)
ctxt = get(app.cfile, 'Value');
uiputfile(ctxt, 'custom.cfile');
But every time I try to run it, rather than generating ctxt, the Text Area value, as the file, it opens the save file dialogue box and lists the lines of text in the Text Area as different file types? Like this:
Do you know what I'm doing wrong/how to resolve this issue?
Rik
Rik le 3 Août 2018
The uiputfile function asks the user for a location to save your file to, it doesn't write the file. With the writing of the file we can't help you because you didn't give a description of the file format.
Hanna Al-Kowsi
Hanna Al-Kowsi le 3 Août 2018
What information about file format is required? The file type is a .cfile, and essentially should just be the lines of text from the Text Area.
Rik
Rik le 3 Août 2018
If it is just a text file with some extension, you should be able to find plenty of examples how to write the contents of a cell to a text file. I could find what type of file it would be, as there isn't even a filext page.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 3 Août 2018

0 votes

savedir = '.'; %current directory? You should probably adjust this to a known location or use uigetdir()
outfile = fullfile(savedir, 'custom.cfile');
ctxt = get(app.cfile, 'Value');
[fid, message] = fopen(outfile, 'wt');
if fid < 0
fprintf('Failed to save file "%s" because: %s"\n', outfile, message);
return
end
fprintf(fid, '%s\n', ctxt{:});
fclose(fid)

Plus de réponses (0)

Catégories

Produits

Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by