Plotting frequency curves in one figure

5 views (last 30 days)
I have 3 different frequency intervalls ( see frequency ranges columns) and I would like to plot them in one figure.
I tried to plot them with this code below but I either get the error that they should have the same vector length or I get the following error:
"Element 2 of the text contains 1 matches while the previous elements have 0. All elements must contain the same number of matches."
Thanks a lot in Advance for the help!
FEC_fb2=FEC_f2.fr;
FEC_fb2 = double(split(string(FEC_fb2(1:end-1)) , ","));
FEC_fb2 = FEC_fb2(:,2);
FEC_fb3=FEC_f3.fr;
FEC_fb3 = double(split(string(FEC_fb3(1:end-1)) , ","));
FEC_fb3 = FEC_fb1(:,3);
FEC_fb1=FEC_f1.fr;
FEC_fb1 = double(split(string(FEC_fb1(1:end-1)) , ","));
FEC_fb1 = FEC_fb1(:,1);
FEC_timefb1 = duration(string(FEC_f1.time));
FEC_timefb2 = duration(string(FEC_f2.time));
FEC_timefb3 = duration(string(FEC_f3.time));
nexttile
plot(FEC_timefb1(1:end-1),FEC_fb1);
xticks([FEC_timefb1(1) FEC_timefb1(end-1)]);
hold on
plot(FEC_timefb2(1:end-1),FEC_fb2);
plot(FEC_timefb3(1:end-1),FEC_fb3);
plot([FEC_timefb1(1) FEC_timefb1(end-1)],[avg_FEC avg_FEC],'b-','LineWidth',2);
title(' var ')
xlabel('Uhrzeit in [HH:MM:SS]')

Accepted Answer

Seth Furman
Seth Furman on 16 Oct 2020
The error we see is happening in the split function because not every string in string(FEC_fb2(1:end-1)) has the same number of commas, referred to as "matches" in the error message.
Hopefully this example demonstrates the issue a little more clearly.
>> split(["a,b";"c,d"],",")
ans =
2×2 string array
"a" "b"
"c" "d"
>> split(["a";"c,d"],",")
Error using split
Element 2 of the text contains 1 matches while the previous elements have 0. All
elements must contain the same number of matches.
You can get around this issue by calling split separately on each string and assigning the results to a cell array, which allows elements of different lengths.
strs = ["a";"c,d"];
strsSplit = cell(1,numel(strs));
for i = 1:numel(strs)
strsSplit{i} = split(strs(i),",");
end
strsSplit
which outputs
strsSplit =
1×2 cell array
{["a"]} {2×1 string}
>> strsSplit{2}
ans =
2×1 string array
"c"
"d"
Alternatively, you can use arrayfun.
>> arrayfun(@(s)split(s,","), ["a";"c,d"], "UniformOutput", false)
ans =
2×1 cell array
{["a" ]}
{2×1 string}

More Answers (0)

Community Treasure Hunt

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

Start Hunting!