Add Unique Double Values to a Column in Mulitple Table

I have imported multiple tables into my matlab environment.
I have added a new column, 'PatientNumber' to the table
RIght now, they all have 'ones'.
How do I give each of my new column a unique identifier in each table?
For instance, PatientNumber column in Table 1 should have 'ones',
PatientNumber column in Table 2 should have 'twos'
Patient Number column in Table 3 should have '3' in all the rows and so on.
%enter the file names with wildcard * for all
filenames = 'Patient_Details*';
files = [pwd '\Data\' filenames '.csv']
%set up the data store
ds = datastore(files,'TreatAsMissing', 'NA', 'Delimiter',',',...
'ReadVariableNames', true, 'ReadSize', 'file');
%read all the data from the datastore
dataAll = readall(ds);
nrow = size(dataAll,1);
dataAll.PatientNumber = ones(nrow, 1);
for i = dataAll.PatentNumber
dataAll.PatientNumber=i.*ones(nrow,1)
i+1
end

 Réponse acceptée

Walter Roberson
Walter Roberson le 23 Mar 2021

0 votes

When you use readall() on a TabularText datastore (which is what is generated for .csv files by default), then all of the data is read into a single table() or timetable() object, with no boundaries between the entries.
You will need to switch from readall() to read(), but also configure the datastore with 'ReadSize', 'file' so that it reads a file at a time. You would then add the appropriate label information and store the content, combining all the content after you have reached the last file.

4 commentaires

I have imported multiple tables into my matlab environment.
As far as MATLAB is concerned, you imported multiple files, and when you used readall(), all of the data from all of the files was joined together into a single table() or timetable() object, with no indication of where one file ends and the next begins.
The easiest way around that is to loop using read() instead of using readall() .
I do not know why Mathworks has not made it easier to put labels on all of the datastore types.
Amadi Gabriel Udu
Amadi Gabriel Udu le 23 Mar 2021
Modifié(e) : Amadi Gabriel Udu le 23 Mar 2021
Although they are no boundaries, the data are time series. So the new 'table' starts at 0,1,2,..,n.
So from where the new time series begin, I intend to change the Patient_Name Column to a new double number.
N.B. I have a myriad of tables
dataAll.PatientNumber = cumsum(dataAll.Time == 0);
Works like magic!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by