Outputting imported files in Matlab App Designer
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
I have created a file import dialogue from on my app Designer. The file I have imported are generally .txt files which I need to do two tasks with.
Firstly, I need to read the output (the imported file) and save it in its .txt format.
Secondly I would like to import the same into Matlab as a table and save the same as such.
Below is the code that I have managed to write on the same
value = app.Select_DLRDropDown.Value;
[file,path]=uigetfile;
DLR_siteA=fullfile(path,file);
site_A =DLR_siteA; % the app runs succesfully upto this point when I press the Select_DLR button but it does not open the file site_A
open("site_A"); % returns 'The variable site_A does not exist'.
How can I be able to save the file that has been imported by this tool? And is it possible to convert it directly to a Mat table first then output it as a table?
Any support is appreciated.
Accepted Answer
Guillaume
on 10 May 2019
0 votes
At no point does your code attempt to import anything.
DLR_siteA=fullfile(path,file);
Ok, this store the full path of the file selected in the DLR_siteA variable.
site_A = DLR_siteA;
As always in matlab, when you do A = B, you copy the content of the B variable into the A variable. In your case, B (DLR_siteA) is the full path of the file, so you've just copied the full path of the file into variable site_A
open("site_A");
As documented, open opens a file in the appropriate application. On windows a text file, will typically be opened in notepad. open does not do import. In any case, what you're passing to open is the filename "site_A" (I'd wager that file doesn't exist), not the content of the site_A variable.
Assuming the file you want to import is a text file. There are many ways you can use. Which one is more appropriate depends on the format of your text file. So if you want more help attach an example file. For well formatted text files, the simplest way to import them is with readtable which would give you the table you desire.
I'm a bit confused by that sentence: I need to read the output (the imported file) and save it in its .txt format. The output of what? And why would you save back the file in the same format it started as?
9 Comments
Lui
on 10 May 2019
Thank you for the answer.
[file,path]=uigetfile; % this opens up the data import dialogue from which I chose a specific file.
% how do I save this impoterd file that I have chosen?
What is the callback function to import, save and rename any file from any location in my computer through the app Designer dialogue box?
if
DLR_siteA=fullfile(path,file);
what is the generic call back function for retrieving and renaming an imported file? Do I have to specify each filename? if that is the case then I may not be able to use the data in a different path from what is coded in the app designer callback. Please advise.
Sample snapshot is attached as the dataset is too large to be attached
Guillaume
on 10 May 2019
I think you're using the word callback to mean something else than what it normally means, so I'm unclear what you mean. A callback is an asynchronous function that you pass to another function so that it can call it back at a later time. It's typically used in GUIs to deal with user actions, or to act on external events.
If you're asking for a function to rename files, it's movefile. For importing files, as said it depends on the format of the files. See the link in my answer. There can't be a general import function (although the import tool tries to do its best) since there a so many different format. A text file is completely different from an excel file.
if that is the case then I may not be able to use the data in a different path from what is coded in the app designer callback.
Matlab is a generic programming language. You can do whatever you want with it. I would generally never hardcode paths in my code, so using data in a different path should never be a problem.
Sample snapshot is attached as the dataset is too large to be attached
Put it on dropbox, google drive, or similar and give a link then. It's unclear if your file is just one long line of numbers or multiple lines. If it's multiple lines, you can always reduce the numbers of lines and attach that. We only need to see the beginning of the file, assuming the rest is the same format.
Lui
on 10 May 2019
Please find the link to the data.
Guillaume
on 10 May 2019
This kind of file is trivially imported into matlab. If you're using R2019a, I'd recommend you use the new readmatrix to import this kind of file:
site_A = readmatrix(DLR_siteA);
site_A = csvread(DLR_siteA);
site_A = readtable(DLR_siteA, 'ReadVariableNames', false);
but I'm not convinced that your data should be stored as a table instead of a matrix.
Thank you. But as you mentioned earlier in your comments I am trying to write callacks for my GUI in the App Designer. for example I have a button called Trial which I need it to do two tasks when I press it. First task is allow me to select file from my computer; The second task is to allow me to load that data into the Matlab workspace as a matrix or as a table.
Lastly I would also like it to save the same variable to be used in another stage.
Below is a snippet of the codeview section of the appdesigner GUI.
% Callback function: TrialButton, UITable
function TrialButtonPushed(app, event)
[file,path] = uigetfile;
filename = fullfile(path,file);
Data= readmatrix(file);
Running the app doesn not result in an error but neither does it give any answer in the matlab workspace. What could be wrong?
Secondly, I have written the following line of code to select help me access a file in the button called select
It does not return to me any values unfortunately.
How do I save the file that I have selected? or how do I see it? I understand this line of code may be describing the path to a file. how do I get to the file?
function select_yearDropDownValueChanged(app, event)
value = app.select_yearDropDown.Value;
filename = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on'); % this is my callback
Adam Danz
on 13 May 2019
"Running the app doesn not result in an error but neither does it give any answer in the matlab workspace. What could be wrong?"
If there isn't an error message, then your "Data" variable is probably successfully created but you're not doing anything with it (as far as we can see).
"I would also like it to save the same variable to be used in another stage."
If you'd like to load the data and then store it in your GUI, you could save the data to the "UserData" property of the GUI so that it is accessible from other GUI components.
Guillaume
on 13 May 2019
Yes, we need to see the rest of the two functions, up to and including the end statement.
If the code you show is all the function code, then yes nothing is going to happen since you don't do anything with the variables you create. Variables created in a function are destroyed once the function terminates. That's the whole point of functions.
There are various way to pass data from one function to another or to the base workspace. In App designer, the simplest is as property of the App (the UserData that Adam mentions is more suited for GUIDE)
Lui
on 18 May 2019
Thank you all for the guidance. I realized I had not specified any file in my initial code. so for any other person in the same problem I did the following for the button Load
% Button pushed function: LoadM2002Button
function LoadM2002ButtonPushed(app, event)
%load file1
[filename, pathname] = uigetfile('*.txt','Please select txt files containing project code info...','MultiSelect','off');
Data=readtable([pathname,filename]);
Then afterwards I did modify my data as desrired
% Retrieve the table
Data=Data(2:end,1:40);
Data=table2array(Data(:,:)); % needed the data as a matruix
finally I strored it in my working directory
[file,path] = uiputfile('*.MAT','Please select or provide .MAT file filename for extracted project code info...');
writematrix(Data,'DataA');
That worked. But the file I got was not a Matlab file. It was a text file. So any help will still be appreciated.
1) You ask the user for a file name and path for the mat file, then completely ignore it and use DataA as the file name.
2) The function to save data as mat file is save. Indeed writematrix only writes text or excel files.
So:
save(fullfile(path, file), 'Data'); %Save Data in the mat file specified by the user.
More Answers (0)
Categories
Find more on Data Import and Export in Help Center and File Exchange
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)