using for loop to read multiple column vectors
显示 更早的评论
I want to use a for loop to read different column vectors which are mentioned with different fn data in attached file and then I want to plot several figures from the output of for loop. Main problem is I don't know how to import fn vector data multiple times in loop and replace it with next fn vector when code complete for previous loop.
I have already imported data from txt file by using import tool and named the columns with variable names. fn is a varible which has name fn 10, fn22, fn32.... these are 5 column vectors.
回答(1 个)
Matt J
2020-6-12
0 个投票
11 个评论
shah nawaz
2020-6-14
Matt J
2020-6-14
Why not as follows
fn=cell{5,1}
for i=1:5
fn{i}=import(___);
end
mean_fn=cellfun(@mean, fn)
shah nawaz
2020-6-14
You can do things like,
fn = {file199 file171 file132 file122 file110 };
mfn=cellfun( @(c)mean(c(:,3)), fn);
You could also take the means of all columns like so,
fn = {file199 file171 file132 file122 file110 };
mfn=cellfun( @(c)mean(c), fn, 'uni',0);
shah nawaz
2020-6-14
That should not matter. For example,
>> fn={rand(4,3), rand(10,3)}
fn =
1×2 cell array
{4×3 double} {10×3 double}
>> mfn=cellfun( @(c)mean(c), fn, 'uni',0)
mfn =
1×2 cell array
{1×3 double} {1×3 double}
or
>> mfncol3=cellfun( @(c)mean(c(:,3)), fn)
mfncol3 =
0.5084 0.6062
shah nawaz
2020-6-15
shah nawaz
2020-6-15
shah nawaz
2020-6-15
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!