error Unrecognized function or variable help me please :(!
显示 更早的评论
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 个评论
Stephen23
2024-2-9
How are you calling the function?
My guess is that you are calling it something like this:
PlotSignal(signalsCell, n)
In which case... what value/s do you expect signalsCell to have?
Please show us exactly how you define its input arrays, and exactly how you call this function.
Torsten
2024-2-9
function PlotSignal(signalsCell, n)
signalsCell=signals(2*n-1:2*n,:);
"signalsCell" is the input to your function "PlotSignal" and you overwrite it by "signals(2*n-1:2*n,:)". This must be wrong.
Time
2024-2-10
Stephen23
2024-2-10
"And what do you mean by overwrite ? How do I fix it ?"
What you are doing, overwriting the input argument SIAGNALSCELL and not definiing SIGNALS anywhere:
function PlotSignal(signalsCell, n)
signalsCell=signals(2*n-1:2*n,:);
What you probably intended to do:
function PlotSignal(signals,n)
signalsCell = signals(2*n-1:2*n,:);
Time
2024-2-10
Stephen23
2024-2-10
"But in the question they tell me the input need to be "signalscell" ."
Sure, is there something stopping you from changing the variable names youself?
Time
2024-2-10
tal
2024-2-10
here:)
Stephen23
2024-2-10
And also show us how you call the function.
tal
2024-2-10
n=3;
load('signals.mat','signals')
PlotSignal(signals,n);
回答(3 个)
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
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
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
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 个评论
tal
2024-2-10
thank you so much for your help ! i really appriciate it . do the way i wrote the function it isnt corect at all ?
"do the way i wrote the function it isnt corect at all ?"
It works when you fix the bugs and call it correctly. For example:
signals = load('signals.mat').signals;
PlotSignal(signals,3)
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
Time
2024-2-13
Walter Roberson
2024-2-13
You would get an error in the second line if you pressed the green Run button to execute the code. When you press the green Run button to execute the code, it executes the function with no input parameters.
You need to go down to the command line and
signals = load('signals.mat').signals;
PlotSignal(signals,3)
Time
2024-2-13
编辑:Walter Roberson
2024-2-13
Walter Roberson
2024-2-13
What is the error message?
Time
2024-2-13
Walter Roberson
2024-2-13
As a test, add a whos command near the top,
function PlotSignal(signalsCell, n)
whos
X=signalsCell(2*n-1,2*n);
Y = cell2mat(signalsCell(2:end));
figure;
plot(X,Y,'k');
end
and see what the result is
Time
2024-2-13
@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
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





