Search code examples
user-interfacematlabworkspace

Pass data from workspace to a function


I created a GUI and used uiimport to import a dataset into matlab workspace, I would like to pass this imported data to another function in matlab...How do I pass this imported dataset into another function....I tried doing diz...but it couldnt pick diz....it doesnt pick the data on the matlab workspace....any ideas??

[file_input, pathname] = uigetfile( ...
{'*.txt', 'Text (*.txt)'; ...
'*.xls', 'Excel (*.xls)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select files');

uiimport(file_input);
M = dlmread(file_input);
X = freed(M);

Solution

  • In your script you have three ways to read the file. Choose one on them depending on your file format. But first I would combine file name with the path:

    file_input = fullfile(pathname,file_input);
    

    I wouldn't use UIIMPORT in a script, since user can change way to read the data, and variable name depends on file name and user.

    With DLMREAD you can only read numerical data from the file. You can also skip some number of rows or columns with

    M = dlmread(file_input,'\t',1,1);
    

    skipping the first row and one column on the left. Or you can define a range in kind of Excel style. See the DLMREAD documentation for more details.

    The filename you pass to DLMREAD must be a string. Don't pass a file handle or any data. You will get "Filename must be a string", if it's not a string. Easy.

    FREAD reads data from a binary file. See the documentation if you really have to do it.

    There are many other functions to read the data from file. If you still have problems, show us an example of your file format, so we can suggest the best way to read it.