필터 지우기
필터 지우기

Adding a column to my table

조회 수: 6 (최근 30일)
Austin
Austin 2023년 9월 15일
댓글: Austin 2023년 9월 15일
I am still quite new to Matlab, but I have a table where I am trying to add a column of 1 through k with k being the number of rows. The code I am using to set up the table is:
P = 'Filepath';
S = dir(fullfile(P,'*.csv'));
S = natsortfiles(S);
for k = 1:numel(S)
F = fullfile(P,S(k).name);
S(k).data = csvread(F,245,230,[245 230 245 230]);
end
then I am trying to add a column at the end with:
S.time = transpose([1:k]);
but it keeps returning this error: Scalar structure required for this assignment.
How can I fix this?
  댓글 수: 2
Stephen23
Stephen23 2023년 9월 15일
편집: Stephen23 2023년 9월 15일
"but I have a table ..."
Where?
The deprecated function CSVREAD returns a numeric matrix, whereas DIR returns a structure array. I do not see anywhere in your code where you would have a table.
"...where I am trying to add a column of 1 through k with k being the number of rows"
The number of rows of what exactly?
Note that k is the loop iterator, which here basically just tells your code to process the 1st, 2nd, 3rd, ... Nth file found by your DIR call. It is therefor unclear what k has to do with "time" or "rows".
If the aim is to add this field to the structure S:
S(1).time = 1;
S(2).time = 2;
S(3).time = 3;
..
S(N).time = N;
then all you are doing is replicating the implicitly defined indices of that structure array.
Note that square brackets are a concatenation operator whereas COLON generates a vector. So with this code:
[1:k]
you generate a vector using the COLON and then concatenate that vector with absolutely nothing else to obtain exactly the same vector. Why do you need to perform a superfluous concatenation of a vector with nothing else to get exactly the same vector?
In any case, lets try to figure out what you want to achieve. Given this structure array:
S(1).name = 'hello.csv';
S(1).date = '2023/09/15';
S(2).name = 'world.csv';
S(1).date = '2023/09/15';
please show the exact output that you want to obtain.
Austin
Austin 2023년 9월 15일
Yes I realized that what I had was not a table around 5 minutes after posting this question. I used the struct2table command to convert it to a table and now I am able to manipulate it like I want. The k relating to time is because each file represents one second and therefore the number of the file is the same as the time in seconds that has passed, so I was just reusing that variable from the for loop. Thank you for your help.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2023년 9월 15일
편집: Cris LaPierre 2023년 9월 15일
You have defined S as a multidimension structure (S(k)), not a table, so in order to add time to your structure, you must now include an index: S(1).time
for k = 1:2
S(k).data = rand(5);
end
S(1).time = 1:10;
S(1)
ans = struct with fields:
data: [5×5 double] time: [1 2 3 4 5 6 7 8 9 10]
S(2)
ans = struct with fields:
data: [5×5 double] time: []

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by