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

2 visualizaciones (últimos 30 días)
%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 comentarios
dpb
dpb el 28 de Jun. de 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})
Enter name of new component: fred
Unrecognized function or variable 'Name'.
>> Name=[];
>> newName=input('Enter name of new component: ','s');
Name{length(Name)+1}=newName; %adding new component
Enter name of new component: Fred
>> Name
Name =
1×1 cell array
{'Fred'}
>> newCur=input('Enter Current values corresponding to the 5 voltages for the new component in mA:'); %%THIS IS WHERE THE ERROR IS POINTED OUT
Enter Current values corresponding to the 5 voltages for the new component in mA:1:5
>> CurData
Unrecognized function or variable 'CurData'.
>> newCur
newCur =
1 2 3 4 5
>> CurData=[CurData;newCur]; %appending row of current values for new component
Unrecognized function or variable 'CurData'.
>> CurData=[];
>> CurData=[CurData;newCur]; %appending row of current values for new component
>> MaxCur=max(max(CurData(2:end,:)));
>> MaxCur
MaxCur =
0×5 empty double matrix
>>
As the above shows, we can't duplicate as you didn't provide a complete sample test code that reproduces the issue.
If I create an empty variable for the missing ones, I don't get an error.
Not a real good way to write the user input, it would probably be more user friendly to just define a file format and let them edit the data in a file that is then read than have to type in at command line where any typo is fatal...
We'd have to see all the code and the error in context to be able to comment...
Sean St Cyr
Sean St Cyr el 28 de Jun. de 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

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Jun. de 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.

Categorías

Más información sobre Composite Components en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by