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

2 vues (au cours des 30 derniers jours)
In the following code, out = cryptoshift(in, shift) : while using a sting in the in argument and shift this by an integer value, it shows 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." How to solve this?
function out = cryptoshift('ABC',5 )
numbers = double(lower(in))-96;
codednumbers = zeros(1,length(numbers));
for i = 1:length(numbers);
if numbers(i) >= 1 & numbers(i) <= 26
codednumbers(i) = mod(numbers(i)+shift-1,26)+1;
else
codednumbers(i) = numbers(i);
end
end
out = char(codednumbers+96);

Réponses (1)

Ameer Hamza
Ameer Hamza le 30 Oct 2020
Modifié(e) : Ameer Hamza le 30 Oct 2020
In MATLAB, the input argument in the function definition must be variables. You are writing constant values. Change the function definition to the following
function out = cryptoshift(in, shift) % <--- use variable names here
numbers = double(lower(in))-96;
codednumbers = zeros(1,length(numbers));
for i = 1:length(numbers)
if numbers(i) >= 1 & numbers(i) <= 26
codednumbers(i) = mod(numbers(i)+shift-1,26)+1;
else
codednumbers(i) = numbers(i);
end
end
out = char(codednumbers+96);
end
and then call it like this
cryptoshift('ABC', 5)

Catégories

En savoir plus sur Encryption / Cryptography dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by