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?
采纳的回答
更多回答(1 个)
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 个评论
Zine
2013-3-25
Image Analyst
2013-3-26
编辑:Image Analyst
2013-3-26
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.
Zine
2013-3-26
Mahdi
2013-3-26
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.
Image Analyst
2013-3-26
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.
Mahdi
2013-3-26
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!
Zine
2013-3-26
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
