excel - How to return dynamically created vectors to the workspace? -
hello i'm trying write function reads type of spreadsheet , creates vectors dynamically it's data returns said vectors workspace.
my xlcs structured rows, in first row there string should become name of vector , rest of rows contain numbers make vector.
here code:
function [ b ] = read_excel(filename) %read_excel function read time series data spreadsheet % contents of first cell know name vector [nr, name]=xlsread(filename, 'sheet1','a2:a2'); % transform string name_str = char(name); % create filename varname=genvarname(name_str); % numbers make vector a=xlsread(filename,'b2:ct2'); % create vector corect name , data eval([varname '= a;']); end
as far can tell vector created corectly, have no ideea how return workspace.
preferably solution should able return indeterminate nr of vectors prototype , want function return nr of vectors of user's choice @ once.
to more precise, vector varname created can use in script, if add:
eval(['plot(',varname,')'])
it plot vector, purposes need vector varname returned workspace persist after script run.
because function, need pass variables create output variable. suggest through struct don't know how many variables want output upfront. change eval
line this:
% create vector correct name , data eval(['b.' varname '= a;']);
now should have struct called b
persists in workspace after running function field names equal dynamically created variable names. example 1 varname
x
, can access in workspace b.x
.
but should think code design, dynamically creating variables names unlikely best way go.
Comments
Post a Comment