error Unrecognized function or variable help me please :(!

9 views (last 30 days)
the question(the part i tried to solve):
Write a function called PlotSignal, the function will display on a graph the signal it receives.
1.1. The function inputs are:
1.1.1 signalsCell – an object of cell array type, (each pair of rows in it represents one letter, see details
At the end of the question(
1.1.2 n – the number of the signal to be displayed
1.2. The function does not return any value.
1.3. The letter n from the array of cells should be displayed on a graph according to the following detail:
1.3.1. The signal color shall be black.
as you see i have an error in my code i dont know how to load the input in the workspace i tried to do
function PlotSignal(signalsCell, n)
signalsCell=signals(2*n-1:2*n,:);
Y=cell2mat(signalsCell);
X=signals(n,:);
figure;
plot(X,Y,'k');
end
the eroor
Unrecognized function or variable 'signalsCell'.
can someone please help me ? even to write all over again the function please?
thanks !
  11 Comments

Sign in to comment.

Answers (3)

Catalytic
Catalytic on 10 Feb 2024
signalsCell=load('signals').signals;
PlotSignal(signalsCell, 3)
function PlotSignal(signalsCell, n)
Headings=signalsCell(:,1);
Table=cell2table(signalsCell(:,2:end)');
plot(Table, 2*n-1,2*n,Color='k');
xlabel(Headings{2*n-1});
ylabel(Headings{2*n});
end

Matt J
Matt J on 10 Feb 2024
Edited: Matt J on 10 Feb 2024
Perhaps as follows?
signalsCell=load('signals').signals;
PlotSignal(signalsCell, 3)
function PlotSignal(signalsCell, n)
signalsCell=num2cell(cell2mat(signalsCell(:,2:end)),2);
X=signalsCell(1:2:end);
Y=signalsCell(2:2:end);
figure;
plot(X{n},Y{n},'o-k');
end

Stephen23
Stephen23 on 10 Feb 2024
Edited: Stephen23 on 10 Feb 2024
That really is very bad data design: one single numeric array would be much better than storing lots of numeric scalars in a huge cell array. Your tutor tried to replicate something like a TABLE... but should just use a TABLE. You will have to unlearn half of what they show you :(
signals = load('signals.mat').signals
signals = 6×56 cell array
Columns 1 through 13 {'time'} {[ 0]} {[ 0.1164]} {[ 0.2327]} {[ 0.3491]} {[ 0.4654]} {[ 0.5818]} {[ 0.6981]} {[ 0.8145]} {[ 0.9308]} {[ 1.0472]} {[ 1.1636]} {[ 1.2799]} {'y1' } {[ 7]} {[ 7.5805]} {[ 8.1531]} {[ 8.7101]} {[ 9.2440]} {[ 9.7475]} {[10.2139]} {[10.6369]} {[11.0106]} {[11.3301]} {[11.5911]} {[11.7899]} {'time'} {[-6.2832]} {[-6.0505]} {[-5.8178]} {[-5.5851]} {[-5.3523]} {[-5.1196]} {[-4.8869]} {[-4.6542]} {[-4.4215]} {[-4.1888]} {[-3.9561]} {[-3.7234]} {'y2' } {[ 4]} {[ 4.1153]} {[ 4.2244]} {[ 4.3214]} {[ 4.4011]} {[ 4.4591]} {[ 4.4924]} {[ 4.4992]} {[ 4.4790]} {[ 4.4330]} {[ 4.3637]} {[ 4.2748]} {'time'} {[-6.2832]} {[-6.1668]} {[-6.0505]} {[-5.9341]} {[-5.8178]} {[-5.7014]} {[-5.5851]} {[-5.4687]} {[-5.3523]} {[-5.2360]} {[-5.1196]} {[-5.0033]} {'y3' } {[50.1000]} {[49.7619]} {[48.7522]} {[47.0846]} {[44.7816]} {[41.8744]} {[38.4022]} {[34.4121]} {[29.9579]} {[25.1000]} {[19.9040]} {[14.4402]} Columns 14 through 25 {[ 1.3963]} {[ 1.5126]} {[ 1.6290]} {[ 1.7453]} {[ 1.8617]} {[ 1.9780]} {[ 2.0944]} {[ 2.2108]} {[ 2.3271]} {[ 2.4435]} {[ 2.5598]} {[ 2.6762]} {[11.9240]} {[11.9915]} {[11.9915]} {[11.9240]} {[ 11.7899]} {[ 11.5911]} {[ 11.3301]} {[ 11.0106]} {[ 10.6369]} {[ 10.2139]} {[ 9.7475]} {[ 9.2440]} {[-3.4907]} {[-3.2579]} {[-3.0252]} {[-2.7925]} {[ -2.5598]} {[ -2.3271]} {[ -2.0944]} {[ -1.8617]} {[ -1.6290]} {[ -1.3963]} {[ -1.1636]} {[ -0.9308]} {[ 4.1710]} {[ 4.0580]} {[ 3.9420]} {[ 3.8290]} {[ 3.7252]} {[ 3.6363]} {[ 3.5670]} {[ 3.5210]} {[ 3.5008]} {[ 3.5076]} {[ 3.5409]} {[ 3.5989]} {[-4.8869]} {[-4.7706]} {[-4.6542]} {[-4.5379]} {[ -4.4215]} {[ -4.3051]} {[ -4.1888]} {[ -4.0724]} {[ -3.9561]} {[ -3.8397]} {[ -3.7234]} {[ -3.6070]} {[ 8.7824]} {[ 3.0072]} {[-2.8072]} {[-8.5824]} {[-14.2402]} {[-19.7040]} {[-24.9000]} {[-29.7579]} {[-34.2121]} {[-38.2022]} {[-41.6744]} {[-44.5816]} Columns 26 through 37 {[ 2.7925]} {[ 2.9089]} {[ 3.0252]} {[ 3.1416]} {[ 3.2579]} {[ 3.3743]} {[ 3.4907]} {[ 3.6070]} {[ 3.7234]} {[ 3.8397]} {[ 3.9561]} {[ 4.0724]} {[ 8.7101]} {[ 8.1531]} {[ 7.5805]} {[ 7.0000]} {[ 6.4195]} {[ 5.8469]} {[ 5.2899]} {[ 4.7560]} {[ 4.2525]} {[ 3.7861]} {[ 3.3631]} {[ 2.9894]} {[ -0.6981]} {[ -0.4654]} {[ -0.2327]} {[ 0]} {[ 0.2327]} {[ 0.4654]} {[ 0.6981]} {[ 0.9308]} {[ 1.1636]} {[ 1.3963]} {[ 1.6290]} {[ 1.8617]} {[ 3.6786]} {[ 3.7756]} {[ 3.8847]} {[ 4]} {[ 4.1153]} {[ 4.2244]} {[ 4.3214]} {[ 4.4011]} {[ 4.4591]} {[ 4.4924]} {[ 4.4992]} {[ 4.4790]} {[ -3.4907]} {[ -3.3743]} {[ -3.2579]} {[ -3.1416]} {[ -3.0252]} {[ -2.9089]} {[ -2.7925]} {[ -2.6762]} {[ -2.5598]} {[ -2.4435]} {[ -2.3271]} {[ -2.2108]} {[-46.8846]} {[-48.5522]} {[-49.5619]} {[-49.9000]} {[-49.5619]} {[-48.5522]} {[-46.8846]} {[-44.5816]} {[-41.6744]} {[-38.2022]} {[-34.2121]} {[-29.7579]} Columns 38 through 50 {[ 4.1888]} {[ 4.3051]} {[ 4.4215]} {[ 4.5379]} {[ 4.6542]} {[ 4.7706]} {[ 4.8869]} {[ 5.0033]} {[ 5.1196]} {[ 5.2360]} {[ 5.3523]} {[ 5.4687]} {[ 5.5851]} {[ 2.6699]} {[ 2.4089]} {[ 2.2101]} {[ 2.0760]} {[ 2.0085]} {[ 2.0085]} {[ 2.0760]} {[ 2.2101]} {[ 2.4089]} {[ 2.6699]} {[ 2.9894]} {[ 3.3631]} {[ 3.7861]} {[ 2.0944]} {[ 2.3271]} {[ 2.5598]} {[ 2.7925]} {[ 3.0252]} {[ 3.2579]} {[ 3.4907]} {[ 3.7234]} {[ 3.9561]} {[ 4.1888]} {[ 4.4215]} {[ 4.6542]} {[ 4.8869]} {[ 4.4330]} {[ 4.3637]} {[ 4.2748]} {[ 4.1710]} {[ 4.0580]} {[ 3.9420]} {[ 3.8290]} {[ 3.7252]} {[ 3.6363]} {[ 3.5670]} {[ 3.5210]} {[ 3.5008]} {[ 3.5076]} {[ -2.0944]} {[ -1.9780]} {[ -1.8617]} {[-1.7453]} {[-1.6290]} {[-1.5126]} {[-1.3963]} {[-1.2799]} {[-1.1636]} {[-1.0472]} {[-0.9308]} {[-0.8145]} {[-0.6981]} {[-24.9000]} {[-19.7040]} {[-14.2402]} {[-8.5824]} {[-2.8072]} {[ 3.0072]} {[ 8.7824]} {[14.4402]} {[19.9040]} {[25.1000]} {[29.9579]} {[34.4121]} {[38.4022]} Columns 51 through 56 {[ 5.7014]} {[ 5.8178]} {[ 5.9341]} {[ 6.0505]} {[ 6.1668]} {[ 6.2832]} {[ 4.2525]} {[ 4.7560]} {[ 5.2899]} {[ 5.8469]} {[ 6.4195]} {[ 7.0000]} {[ 5.1196]} {[ 5.3523]} {[ 5.5851]} {[ 5.8178]} {[ 6.0505]} {[ 6.2832]} {[ 3.5409]} {[ 3.5989]} {[ 3.6786]} {[ 3.7756]} {[ 3.8847]} {[ 4]} {[-0.5818]} {[-0.4654]} {[-0.3491]} {[-0.2327]} {[-0.1164]} {[ 0]} {[41.8744]} {[44.7816]} {[47.0846]} {[48.7522]} {[49.7619]} {[50.1000]}
PlotSignal(signals,1)
PlotSignal(signals,3)
function PlotSignal(inp,n)
sig = "y"+n;
idx = find(strcmpi(sig,inp(:,1)));
X = cell2mat(inp(idx-1,2:end));
Y = cell2mat(inp(idx-0,2:end));
plot(X,Y,'+-k');
legend(sig)
end
  11 Comments
Voss
Voss on 13 Feb 2024
@Time: I get a different error (using the mat file provided by @tal):
n=3;
load('signals.mat','signals')
PlotSignal(signals,n);
Error using cell2mat
All contents of the input cell array must be of the same data type.

Error in solution>PlotSignal (line 7)
Y = cell2mat(signalsCell(2:end));
function PlotSignal(signalsCell, n)
X=signalsCell(2*n-1,2*n);
Y = cell2mat(signalsCell(2:end));
figure;
plot(X,Y,'k');
end
Voss
Voss on 13 Feb 2024
Edited: Voss on 13 Feb 2024
@Time: However, if I modify the function as suggested by Stephen, it runs fine:
n=3;
load('signals.mat','signals')
PlotSignal(signals,n);
function PlotSignal(signalsCell, n)
X = cell2mat(signalsCell(2*n-1,2:end));
Y = cell2mat(signalsCell(2*n-0,2:end));
plot(X,Y,'k');
end

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!