I have a 4x1 cell named D that consists of:
D{1,1} = 712x10 int32
D{2,1} = 712x10 int32
D{3,1} = 866x10 int32
D{4,1} = 854x10 int32
Is there anyway to separate cells into individual matrices? I want to manipulate columns 8 and 9 of the individual matrices but when i try to use cell2mat it merges all the cells together into one large matrix. Is there a way to do it for any number of cells? For example if there were 100 instead of 4? Here is the code i have so far:
function [D,StepInfo] = ImportSpecialTextFile(filename)
fidi = fopen(filename,'rt');
k1 = 1;
while ~feof(fidi)
for k2 = 1:3
StepInfo{k1,k2} = fgetl(fidi);
end
C = textscan(fidi, ['R' repmat('%d',1,10)], 'Delimiter',',', 'CollectOutput',true);
M = cell2mat(C);
if isempty(M)
break
end
D{k1,:} = M;
Dd{k1,:} = cell2mat(D(k1));
fseek(fidi, 0, 0);
k1 = k1 + 1;
end
fclose(fidi);
end
With a call to the function:
filename = 'extractcdb.txt';
[D,StepInfo] = ImportSpecialTextFile(filename)

8 件のコメント

Stephen23
Stephen23 2019 年 6 月 28 日
編集済み: Stephen23 2019 年 6 月 28 日
"Is there anyway to separate cells into individual matrices?"
Sure, that is easy:
W = D{1,1};
X = D{2,1};
Y = D{3,1};
Z = D{4,1};
or
[W,X,Y,Z] = deal(D{1:4,1});
"I want to manipulate columns 8 and 9 of the individual matrices..."
That is easy, just use a loop and indexing. Indexing is simple and very efficient.
"...but when i try to use cell2mat it merges all the cells together into one large matrix"
Then don't use cell2mat! Just use indexing to access the cell array contents:
"Is there a way to do it for any number of cells? For example if there were 100 instead of 4?"
Use indexing.
There are certainly ways to create individual variables, but they are not recommended. Doing so requires dynamically naming variables, which is one way that beginners force themselves into writing slow, complex, obfuscated, buggy, code which is hard to debug. Read this to know why:
Your cell array is perfect, there is no reason for you to pointlessly split up that cell array into lots of little arrays that would just force you into writing bad code. You should use indexing. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).
Bradley Sprinkle
Bradley Sprinkle 2019 年 6 月 28 日
Thank you, but is there a way that i can do it for more than just 4? With the data file given, the code separates the 'R' lines from the 'I,V,D' lines but the code should be able to work for larger data files. Is there a way to efficiently do that?
Stephen23
Stephen23 2019 年 6 月 28 日
編集済み: Stephen23 2019 年 6 月 28 日
"Is there a way to efficiently do that?"
Yes, using indexing is efficient.
Fundamentally the approach of dynamically defining variables is NOT efficient.
Indexing is efficient. What you are trying to do is not efficient, as well as being more complex, difficult to read and maintain, and harder to debug. That is why I advised you to use indexing.
"...but the code should be able to work for larger data files."
Sure, so use indexing. What is stopping you?
Bradley Sprinkle
Bradley Sprinkle 2019 年 6 月 28 日
I may be explaining what im trying to do in a poor way or maybe just misunderstanding. Will indexing work for more than 4 sections of R lines?
Stephen23
Stephen23 2019 年 6 月 28 日
編集済み: Stephen23 2019 年 6 月 28 日
"Will indexing work for more than 4 sections of R lines?"
Of course, why not? Just define the loop to iterate over as many sections as D has:
for k = 1:numel(D)
D{k} % do whatever you want with that matrix
end
I just tried it on your example imported data:
>> for k = 1:numel(D), mean(D{k}), end
ans =
356.5 0 1 1 2.1398e+05 34.98 15256 19614 18528 45864
ans =
356.5 0 0.91573 0.9059 2.1393e+05 34.98 15302 14728 13908 45840
ans =
433.5 0 0.99885 0.99885 2.5979e+05 34.993 264.87 17045 16074 45752
ans =
427.5 0 0.99883 0.99883 2.5641e+05 37.991 345.33 17179 17216 49965
There is no restriction on the number of "sections", or how many rows they should have...
Bradley Sprinkle
Bradley Sprinkle 2019 年 6 月 28 日
Thank you very much. Is there a way to only manipulate the 8th and 9th columns and hide the others?
Stephen23
Stephen23 2019 年 6 月 28 日
"Is there a way to only manipulate the 8th and 9th columns..."
Sure, just use indexing, e.g. D{k}(:,[8,9])
Or using your example imported data:
>> for k = 1:numel(D), mean(D{k}(:,[8,9])), end
ans =
19614 18528
ans =
14728 13908
ans =
17045 16074
ans =
17179 17216
"... and hide the others?"
I have no idea what it means to "hide" a column of a matrix.
Bradley Sprinkle
Bradley Sprinkle 2019 年 6 月 28 日
Thank you for all of your help!

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2019 年 6 月 28 日

0 投票

This looks familiar!
‘I want to manipulate columns 8 and 9 of the individual matrices ...’
To do that for ‘D{1}’:
D1_Cols89 = D{1}(:,[8 9]);
Thjis creates an (Nx2) array. Call it whatever you wish.
You can put those into a loop as well, and do whatever you want iteratively. For example to calculate the dot product of columns 8 and 9 for each ‘D’:
for k = 1:numel(D)
DotCols(k,:) = dot(D{k}(:,8), D{k}(:,9)); % Dot Product Of Columns 8 & 9
end
If you expect vector outputs, and therefore different size results of the calculation, use cell indexing for the result (using {k} instead of (k)) instead.

4 件のコメント

Bradley Sprinkle
Bradley Sprinkle 2019 年 6 月 28 日
編集済み: Bradley Sprinkle 2019 年 6 月 28 日
I used this code to find the first order differences of each of the 2 columns and I want to add those first order differences back to the original values. But it seems to throw an error usint vertcat. is it simply something wrong with my syntax?
for k = 1:numel(D)
D{k} % do whatever you want with that matrix
B = [diff(D{k}(:,[8,9]))] * 1.2;
B =[zeros(length(D{k}) - length(B),1);B];
C = cumsum(B) + (D{k}(:,[8,9]))
end
Star Strider
Star Strider 2019 年 6 月 28 日
Please describe what you want to do.
If you want to calculate the differences between Columns 8 and 9, just subtract them:
B = (D{k}(:,8) - D{k}(:,9)) * 1.2;
That will keep the row size consistent wit the original row sizes, as well. (The output of the diff call will be one element shorter than the size of its argument, in whatever dimension you select.)
What do you want ‘C’ to be?
Bradley Sprinkle
Bradley Sprinkle 2019 年 6 月 28 日
I want to individually find the first order differences of columns 8 and 9. Take the first order difference and increase it by a percentage (for example, 20%) and then add that value back to the original value to get the increase. For example if one row was 100 and the next next row was 50. the difference should be -50 and then an increase of 20% would make the difference -60. Adding that back should get a new value of 40. My previous code for it was :
A = textscan(fid, '%*s %*s %*s %*f %*f %*d %*f %*d %*d %d %*d','Delimiter',',');
Ad = cell2mat(A);
B = [diff(Ad)]*1.2;
B =[zeros(length(Ad) - length(B),1);B];
for i = 1
z = 1:708
C = cumsum(B(i+z)) + Ad(i)
end
But this code is specific to one section of R lines which i am trying to make it apply to each indiviual section
Star Strider
Star Strider 2019 年 6 月 28 日
I still have no idea what you want. I specifically do not understand using diff and then using cumsum.
This will at least run without error:
for k = 1:numel(D)
B = [diff(D{k}(:,[8,9]))] * 1.2;
B =[0 0;B];
C = cumsum(B) + (D{k}(:,[8,9]))
end
If you just want to add ‘B’ to columns 8 & 9, this works:
for k = 1:1%numel(D)
B = [diff(D{k}(:,[8,9]))] * 1.2;
B =[0 0;B];
C = B + (D{k}(:,[8,9]));
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Time Series Events についてさらに検索

質問済み:

2019 年 6 月 28 日

コメント済み:

2019 年 6 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by