MATLAB Answers

Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

4 views (last 30 days)
Sean St Cyr
Sean St Cyr on 28 Jun 2020
Answered: Walter Roberson on 28 Jun 2020
%New Component
newName=input('Enter name of new component: ','s');
Name{length(Name)+1}=newName; %adding new component
newCur=input('Enter Current values corresponding to the 5 voltages for the new component in mA:'); %%THIS IS WHERE THE ERROR IS POINTED OUT
CurData=[CurData;newCur]; %appending row of current values for new component
MaxCur=max(max(CurData(2:end,:)));
[R,C]=find(CurData(2:end,:)==MaxCur);
fprintf('Maximum Current = %d mA\n',MaxCur)
fprintf('At voltage = %d V\n',CurData(1,C))
fprintf('For %s\n',Name{R})
I have this code and it give me the error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses. At the Line that is labeled. What am I missing?
  2 Comments
Sean St Cyr
Sean St Cyr on 28 Jun 2020
clear all
clc
Name = {'Holtz100','Lever014','Dillard202'}
CurData= [5,7,10,12,15;128,142,165,180,212;18,20,23,25,30;260,285,333,368,428]
%UserSelections
choice=menu('Select a Component Name',Name);
Power=CurData(1,:).*CurData(choice+1,:)*10^-3;%multiplying by 10^-3 to covert mA into A
fprintf('Voltage Choices:\n')%showing the choices
fprintf('%d\t\t',CurData(1,:))
fprintf('\n')
Volt = menu('Select a Voltage', num2cell(CurData(1, :))); %converting the numbers into string so the person may choose it
Voltage = CurData(1, Volt);
Current = CurData(choice+1, Volt);
Component = Name(choice);
fprintf('Component %s\n', string(Component));
fprintf(' \tVoltage = %d V \n', Voltage);
fprintf('\tCurrent = %.3f A\n',CurData(choice+1, Volt)*10^-3); %Converts mA to A
fprintf('\tPower = %.3f W\n',Power(Volt)); %Gives the first power answer instead of the string
%New Component
newName=input('Enter name of new component: ','s');
Name{length(Name)+1}=newName; %adding new component
newCur=input('Enter Current values corresponding to the 5 voltages for the new component in mA:');
CurData=[CurData;newCur]; %appending row of current values for new component
MaxCur=max(max(CurData(2:end,:)));
[R,C]=find(CurData(2:end,:)==MaxCur);
fprintf('Maximum Current = %d mA\n',MaxCur)
fprintf('At voltage = %d V\n',CurData(1,C))
fprintf('For %s\n',Name{R})
Here is the code in its entirety if this helps

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 28 Jun 2020
If the user enters a vector of numbers separated by spaces or commas, in response to an input() statement that does not have an 's', then that a syntax error for input()
... I was certain it didn't used to work that way, but now that I go back to test, it seems to have been true since at least R2015b.
So... use the 's' option to input, and parse the result such as with sscanf() . And make sure to count the number of values before trying to do the next line, as the user might not have entered enough values.

Community Treasure Hunt

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

Start Hunting!

Translated by