import data using GUIs

Hello gents, I have some data to process in MATLAB, my data are in xlsx files. I want to create a user interface to process this data by first importing it using the same GUI and then plot the results, my question is how to import data using gui?

 采纳的回答

Image Analyst
Image Analyst 2013-3-24

1 个投票

You might try a listbox arrangement like this: http://www.mathworks.com/matlabcentral/fileexchange/24224. It gives a good GUI framework for starting something like this.
You could fill the listbox with .xlsx files, then when the user clicks on one, it reads it in and plots the results in an axes. You don't have to use image files and display them. You could adapt it to xlsx files and plot them.

更多回答(1 个)

Mahdi
Mahdi 2013-3-25

1 个投票

I would suggest using the uigetfile function and then using the load function by using a pushbutton.
Using GUIDE, make a pushbutton and go to the callback, then type
% This lets you navigate to the file that you want.
[filename, pathname]=uigetfile
Path=stract(pathname,filename)
YourFile=load(Path)
Basically, your data now has the string YourFile in a matrix from the .xslx file.

7 个评论

I would like to thank Image Analys and Mahdi about these kind answers, the way I wanted is as described by Mahdi but I tried several times with 'stract' and 'struct' and other ways in MATLAB help but I could not achieve what I want, the dialogue box appears and I choose the file name but when closing it returns error message and no data retrieved, I would like if you explain to me some more because I am very biginner in MATLAB, my data are in excel file in 2 col, and I want to name the first col as xdata and the second as ydata, thanks very much in advance and sorry for disturbance.
Several problems with Mahdi's code. There is no "stract" function, and struct wouldn't have been right either. He should have put "fullfile" instead. Also the load() is not what you want either. Documentation for load doesn't say anything about it being able to understand Excel format files. You should use xlsread(), which is meant for this:
[num, txt, raw] = xlsread(fullFileName);
I do not recommend using uigetfile unless you want a tedious user interface or do not have a bunch of xls files to process. I'd recommend a listbox (like I showed) if you have a bunch of xls files that the user might want to select from.
Next, and most importantly, you should not use Path as the name of one of your variables, since that is a built-in reserved variable in MATLAB that you definitely don't want to blow away by replacing it with the name of your user's file.
Finally, this does not make sense: "your data now has the string YourFile in a matrix from the .xslx file". Your data does not have the string 'YourFile' in it. Your data is in the workbook and needs to be transferred to a MATLAB variable. If you used xlsread, you'd have one or more cell arrays. None of them would have a string YourFile in them. Two of the arrays (txt and raw) would have cells, where the cell array cells' contents are the contents of the Excel cells. Two cell arrays (num and raw) would have the numbers from the Excel cells in the cell array cells.
Is there some reason why you can't just replace the boilerplate code in the function I gave you with your own code? It does a lot of the heavy lifting for you so you don't have to learn it all from scratch.
Thanks very much Image Analyst, the reason to do not use a list of defined files is that my program analyse and process data and return results, the origin of this data is the user himself, he will bring his own data in memory stick or on his own computer and he runs the program to analyse so he should have freedom to choose his file name and path, I am trying what you seggested
Hey, Sorry. The stract was a typo. it should be strcat (combine strings). This step was to make sure that if the excel file is in a different directory, it can still be used. (I'm sure there's a simpler way to do this, but it's slipping me at the moment)
I am assuming that if Zine wants to process many files at a time, he can use a loop instead of uigetfile.
Yeah, definitely an amateur mistake to use Path; forgot that it was a reserved MATLAB variable.
That's weird, I checked loading a .xlsx before and it worked, but I was definitely wrong. Use xlsread as a solution.
This is what I would suggest it would look like (if Zine still wants to do it by importing a file each time)
[filename, pathname]=uigetfile
FileNameString=strcat(pathname, filename) %Same as FullFileName
DataFromFile=xlsread(FileNameString)
The variable DataFromFile now contains a matrix with the data in sheet one. Type doc xlsread to understand this more.
Please point out any mistakes, I'm definitely not as experienced as Image Analyst in MATLAB.
Sometimes folders have trailing slashes and sometimes they don't. That's why I use fullfile() - it figures that out and does the right thing. With strcat, you have to make sure it has a trailing slash, so you're going to have to have another line of code to check for that. If you know for a fact that the folder, like what uigetfile() returns, then you're okay. But realize that all folders do not have trailing slashes. Not even the Mathworks is consistent in this. For example, if you add this line after your uigetfile line:
[folder, baseFileName, extension] = fileparts(FileNameString)
you'll see that folder in this case DOES NOT have a trailing slash. So to avoid worrying about whether a trailing slash is there or not, I use fullfile() - it's just more robust and easier.
Thanks! I had no idea fileparts even exists or the trailing slash issue. That line definitely helps with writing my codes as well and it makes sense!
Thanks very much gents, your discussion was very very helpful for me and I can set my question to solved, even it answers some other questions about the subject that I wanted to ask later, your answers are both accepted and I will tick them if possible

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by