Unable to perform assignment because brace indexing is not supported for variables of this type.

1 view (last 30 days)
How should I define my variables before the loop to get this to work??? Please help I am lost!
clear;clc;
%%
fish = input('How much do your fish weigh(in kg as a vector[])? ');
grade = char(1,length(fish));
price = char(1,length(fish));
use = char(1,length(fish)); % preallocate cell array.
for k = 1: length(fish)
if fish(k) >= 30
grade{k} = 'S';
price{k} = 20*fish;
use{k} = 'Whole Fish';
elseif fish(k) > 20
grade{k} = 'A';
price{k} = 15*fish;
use{k} = 'Fillet';
elseif fish(k) > 10
grade{k} = 'B';
price{k} = 6*fish;
use{k} = 'Canning';
elseif fish(k) > 5
grade{k} = 'C';
price{k} = 4*fish;
use{k} = 'Pet Food';
else
grade{k} = 'D';
price{k} = 1*fish;
use{k} = 'Fish Extracts';
end
end
totalPrice = sum(price(:));
fprintf('Grade Use Weight Total Income\n')
fprintf('%4c %15c %5d kg $%-d\n',[grade;use;fish;price])
fprintf(' Total Income: $%d\n',totalPrice)

Answers (2)

Matt J
Matt J on 29 Oct 2019
Edited: Matt J on 29 Oct 2019
grade = cell(1,length(fish));
price = cell(1,length(fish));
use = cell(1,length(fish)); % preallocate cell array.
Also, these lines should be
grade{k} = 'D'; %'k' not 'i'
price{k} = 1*fish;

Walter Roberson
Walter Roberson on 29 Oct 2019
grade = char(1,length(fish));
That is going to make grade into a character vector.
grade{k} = 'S';
That needs grade to be a cell array of character vectors.
grade(k) = 'S';
should work.

Community Treasure Hunt

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

Start Hunting!