I'm writing a code that extracts data from individual subjects (> 100 subjects) and specific days (variavles). In my code, how can I tell matlab to pick a specific variable to write in excel when I have multiple variables? Thanks everyone!

clear all
close all
clc
%define a file name (age of the subjects) for a new or existing excel file that you would like
%data exported to
ExportFile=input('Name of excel file to export to ','s');
%Test to see if the specified file name exists
ExportFileExists=[ExportFile,'.xls'];
ExistTest=exist(ExportFileExists, 'file');
%Adds data if the specified file name to export data exists
Subject_Ageinput = 'string';
%if ExistTest>=1
%Asks user to define the animal number
Animal_Ageinput=input('Animal_Number(nnn.n)','s');
%end
%Reads subject data from specified excel file
disp 'Reading MUNE Data...'
Animal_Ageinput22=xlsread(Subject_Ageinput,'A2:M2');
Animal_Ageinput24=xlsread(Subject_Ageinput,'A3:M3');
Subject_Ageinput26=xlsread(Subject_Ageinput,'A4:M4');
Subject_Ageinput28=xlsread(Subject_Ageinput,'A5:M5');
Subject_Ageinput30=xlsread(Subject_Ageinput,'A6:M6');
Subject_Ageinput32=xlsread(Subject_Ageinput,'A7:M7');
Subject_Ageinput34=xlsread(Subject_Ageinput,'A8:M8');
Subject_Ageinput36=xlsread(Subject_Ageinput,'A9:M9');
Subject_Ageinput38=xlsread(Subject_Ageinput,'A10:M10');
Subject_Ageinput40=xlsread(Subject_Ageinput,'A11:M11');
Subject_Ageinput42=xlsread(Subject_Ageinput,'A12:M12');
Subject_Ageinput44=xlsread(Subject_Ageinput,'A13:M13');
Subject_Ageinput46=xlsread(Subject_Ageinput,'A14:M14');
Subject_Ageinput48=xlsread(Subject_Ageinput,'A15:M15');
Subject_Ageinput50=xlsread(Subject_Ageinput,'A16:M16');
Subject_Ageinput60=xlsread(Subject_Ageinput,'A17:M17');
Subject_Ageinput70=xlsread(Subject_Ageinput,'A18:M18');
Subject_Ageinput80=xlsread(Subject_Ageinput,'A19:M19');
Subject_Ageinput90=xlsread(Subject_Ageinput,'A20:M20');
disp 'Done'
%PLEASE NOTE: The code exports Date in excel's serial date number format.
%To convert this number to an actual date, you must change the date type to number
%Age of data to be exported
ExportAge=input('Data age to be exported ','s');
if ExistTest>1
Header={'Subject ID' 'DOB' 'Date' 'Age' 'B-P (mV)' 'P-P(mV)' 'RNS 10' 'RNS 20' 'RNS 50' 'SMUP(uV)' 'SMUP#' 'MUNE' 'Weight'};
data= (vertcat(Subject_Ageinput22));
THIS IS WHERE I NEED HELP!!
I know can write in all of the age variables at this point, however this does not help me.
Is there a code I can use to which allows me to pick any variable and then write it in excel?
data=num2cell(data);
ExportFile=[ExportFile,'.xls'];
previousdata = xlsread(ExportFile,'Data');
previousdata = num2cell(previousdata);
data = vertcat(Header,previousdata,data);
xlswrite(ExportFile,data,'Data');
disp 'done'
%Set headers for the new excel file if the file does not exist
%Exporting data to new excel file
elseif ExistTest==0
Header={'Subject ID' 'DOB' 'Date' 'Age' 'B-P (mV)' 'P-P(mV)' 'RNS 10' 'RNS 20' 'RNS 50' 'SMUP(uV)' 'SMUP#' 'MUNE' 'Weight'};
disp 'Creating new excel document...'
data= (horzcat(Subject_Ageinput22));
THE same thing would need to happen at this point as well.
Is there a code I can use to which allows me to pick any variable and then write it in excel?
data=num2cell(data);
data=vertcat(Header,data);
ExportFile=[ExportFile,'.xls'];
xlswrite(ExportFile,data,'Data');
disp 'done'
end
Thanks for all of your help.

2 Comments

I have also attached a file which shows how I want my code to work. Again thanks for all of your help!
Variable names with numbers are always a bad sign... because beginners try to access them dynamically, exactly like you want to now. Bad idea. This leads to slow, buggy, and obfuscated code. Here is why:
The solution is very simple: do NOT create lots of numbered variables, instead you should put the data into one array.

Sign in to comment.

Answers (2)

You ask "how can I tell matlab to pick a specific variable to write in excel?". Well, that's exactly what you already did when you told it to write the variable called "data" to a worksheet called "Data" in this line
xlswrite(ExportFile,data,'Data');
Are you asking if you can have the user input some name of a variable that is to be written out? Please clarify.

1 Comment

Yes, so I want the user to be able to input any one of these variables: Animal_Ageinput22 Animal_Ageinput24 Animal_Ageinput26, etc... each time the code is run

Sign in to comment.

The solution is easy: don't create lots of numbered variables. No matter how much beginners love them, numbered variables are a really bad way to program. Instead put all of your data into one numeric array (or cell array), and then your task become trivial because you can use indexing to pick the data you need.
Poor programming decisions make programs slow, obfuscated, and buggy. Avoid numbered variables.
For your code instead of these:
Animal_Ageinput22=xlsread(Subject_Ageinput,'A2:M2');
Animal_Ageinput24=xlsread(Subject_Ageinput,'A3:M3');
Subject_Ageinput26=xlsread(Subject_Ageinput,'A4:M4');
Subject_Ageinput28=xlsread(Subject_Ageinput,'A5:M5');
... etc
you should do something like this one line:
data = xlsread(Subject_Ageinput,'A1:M20');
I put all of the data into one variable and replaced twenty lines of your code with one much simpler line. Now you can use indexing to get any rows of data that you want. Easy. Why make your life more complicated than it need be?
Here is why dynamically accessing variables is slow, buggy and obfuscated way to write code:

2 Comments

Awesome loving all the help! So if I were to use this. How would I write to excel for one specific line in the array? Would it be:
xlswrite(ExportFile,data,'A1:M2','Data'); ?
What you suggest does not seem to match any of the syntaxes described in the xlswrite documentation: perhaps you swapped the sheet and xlRange inputs.
To use one line of the data matrix you need to do something like this:
k = 3; % pick your row of the data matrix
xlswrite(filename, data(k,:), sheet, xlRange)
Matrix indexing is explained very well in the introductory tutorials:

Sign in to comment.

Asked:

on 2 Feb 2016

Edited:

on 5 Feb 2016

Community Treasure Hunt

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

Start Hunting!