How to give range of cells different variable names?

Hello!
I am collecting data from a range of cells from an .xlsx file using the readmatrix function as so:
Datafiles(j).data = readmatrix(Data,'Sheet','Report','Range','M18:N18','FileType','spreadsheet');
Then I populate that data into a new excel file like so:
CurrentData = {Datafiles.data}';
TABLE_Pass = table(CurrentData);
TABLE = vertcat(TABLE_Pass);
Now, my problem is, it spits out the data in 2 columns labeled "CurrentData_1" "CurrentData_2" and I want to specify the name of the second column.
When I create a second variable name, it gives me 4 columns with the information doubled.
Any tips on how to give the data 2 separate variable names?

 Réponse acceptée

Star Strider
Star Strider le 12 Août 2024

0 votes

It might help to have the file.
Otherwise, conbbsider specifying the variable names as (for example):
TABLE.Properties.VariableNames = {'Variable Name 1','Variable Name 2'}
of course using the varialbe names you want to assign to them.
.

4 commentaires

KD
KD le 13 Août 2024
This gives me the same problem I have with the current code I'm using. I can't have 2 different variable names because both sets of data are coming from
Datafiles(j).data = readmatrix(Data,'Sheet','Report','Range','M18:N18','FileType','spreadsheet');
and if I create
CurrentData = {Datafiles.data}';
CurrentData2 = {Datafiles.data}';
It just doubles the information I need.
Any thoughts on how I can either 1. make them different variables or 2. make one variable equal just M18 and the other equal just N18?
Thanks!
It would help to have the data file so I can see if I can reproduce that problem and find a fix for it.
Is ‘Data’ an Excel file or something else?
KD
KD le 13 Août 2024
This is the file it's current spitting out. I want to be able to name the columns, so they actually represent the data beneath them. "data" is the path where the excel files are located.
I don’t know what they should be called, however this is how to change their names. Insert the correct names for ‘Column 1’ and ‘Column 2’. (In R2023b, spaces and some other special characters are permitted.)
Data = 'CurrentData.xlsx';
data = readtable(Data)
data = 4x2 table
CurrentData_1 CurrentData_2 _____________ _____________ 6.4 0.5 6.5 0.7 6.1 0.2 6.3 0.1
data.Properties.VariableNames = {'Column 1','Column 2'}
data = 4x2 table
Column 1 Column 2 ________ ________ 6.4 0.5 6.5 0.7 6.1 0.2 6.3 0.1
.

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 12 Août 2024

0 votes

Try replacing this
CurrentData = {Datafiles.data}';
TABLE_Pass = table(CurrentData);
TABLE = vertcat(TABLE_Pass);
with this
CurrentData = vertcat(Datafiles.data);
TABLE = array2table(CurrentData,'VariableNames',{'x','y'});
and replace 'x' and 'y' with the variable names you want to use.

2 commentaires

KD
KD le 13 Août 2024
This gives me the same problem I have with the current code I'm using. I can't have 2 different variable names because both sets of data are coming from
Datafiles(j).data = readmatrix(Data,'Sheet','Report','Range','M18:N18','FileType','spreadsheet');
and if I create
CurrentData = {Datafiles.data}';
CurrentData2 = {Datafiles.data}';
It just doubles the information I need.
Any thoughts on how I can either 1. make them different variables or 2. make one variable equal just M18 and the other equal just N18?
Thanks!
Voss
Voss le 13 Août 2024

Don't create a cell array of 1-by-2 vectors by doing this:

CurrentData = {Datafiles.data}';

because that will lead to a table with one variable, which is a cell array of 1-by-2 vectors.

Instead, create an N-by-2 matrix by doing what I showed in my answer:

CurrentData = vertcat(Datafiles.data);

because that will lead to a table with two variables, which you can name individually.

Connectez-vous pour commenter.

Produits

Version

R2023b

Question posée :

KD
le 12 Août 2024

Commenté :

le 13 Août 2024

Community Treasure Hunt

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

Start Hunting!

Translated by