How do I write several variables to a table with row labels?

(A disclaimer, my MatLab knowledge is very rudimentary, so please forgive me if I don't use the proper terminology or understand certain terminology)
I'm working with large datasets of values of a flourescent signal over time. My lab already has some code that does a number of calculations. When we run this, it gives a fairly large list of variable values in the workspace. There are 4 of those variables we regularly use (MeanAmplitude, AmpError, MeanFrequency, and FrequencyError). I'm trying to figure out what code I need to add so that when I run the code to process the data, it saves an Excel file with a simple table with variable names in column A (labeling the rows) and the values for each variable (as pictured).
I've tried various forms of "xlswrite" and "writetotable" etc, but I keep getting error codes. Aslo, if it is easier to do this with the row labels being the full name of the variables (as listed above), that is fine

 Accepted Answer

% Your data
MeanAmplitude = rand();
AmpError = rand();
MeanFrequency = rand();
FrequencyError = rand();
% Name can be any string here (full or short)
rn = ["MeanAmplitude"; "AmpError"; "Mean Frequency"; "Frequency Error"];
val = [MeanAmplitude; AmpError; MeanFrequency; FrequencyError];
t = [array2table(rn) array2table(val)];
writetable(t, "test.xlsx")
ls
test.xlsx

4 Comments

Thanks so much for this. This is what I needed. The "rand()" part. I had saw when I was trying to find a solution. After seeing that this code worked, I tried to read up about rand() but no explanations or documentation I saw had examples like this with nothing in the parentheses. Things discuss it being a "random" variable, but does that just mean it can be any number until you define it? And in this case it's defined as "any" number equal to the variable needed (AmpError, etc), and because that variable has a single set value, that's what it returns?
Wait, nevermind, keeping "rand()" in there literally generates random values for these and not the already calculated values for the variables. Maybe you put that in there as a placeholder? It works just commenting that out, though.
"Maybe you put that in there as a placeholder?"
Correct. Becauser you did not provide any sample data Chunru invented some fake data using RAND(). Code without data will not run, so what data do you expect Chunru to use to test their code?
Of course you use your own data, exactly as the "% Your data" comment tells you.
Note that calling TABLE would be simpler:
rn = ["MeanAmplitude"; "AmpError"; "Mean Frequency"; "Frequency Error"];
val = [MeanAmplitude; AmpError; MeanFrequency; FrequencyError];
tbl = table(rn,val)
writetable(tbl, "test.xlsx")
"so what data do you expect Chunru to use to test their code?"
Fair enough. Given that my other attempts to do this had issues, I how I was referencing these variables that were defined elsewhere was the issue, and maybe "RAND()" was how to reference them properly.
Good tip on the table too by the way. Thanks!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 14 Nov 2023

Commented:

on 14 Nov 2023

Community Treasure Hunt

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

Start Hunting!