Not able to see where the index is going past 5

2 views (last 30 days)
load('Motor_Data.mat');
fprintf('Available motors:\n');
for i = 1:length(Names)
fprintf('%d: %s\n', i, Names(i));
end
motor_index = input('Select a motor by entering its number: ');
mass = input('Enter the mass to be lifted (in kg): ');
height = input('Enter the height to lift the mass (in meters): ');
%Says this is where it errors
motor_name = Names(motor_index); %HERE
power = Specs(1, motor_index);
efficiency = Specs(2, motor_index);
% Calculate output power
efficiency_decimal = efficiency / 100;
output_power = efficiency_decimal * power;
g = 9.81;
energy_required = mass * g * height;
time = energy_required / output_power;
fprintf('Motor: %s\n', motor_name);
fprintf('Load lifted: %.2f kg\n', mass);
fprintf('Height lifted: %.2f meters\n', height);
fprintf('Time to lift the load: %.2f seconds\n', time);
%Task 2
mass = input('Enter the mass to be lifted by all motors (in kg): ');
height = input('Enter the height to lift the mass by all motors (in meters): ');
% Initialize results matrix
num_motors = length(Names);
results = zeros(5, num_motors);
% Calculate time for each motor
g = 9.81; % Gravity constant
for i = 1:num_motors
power = Specs(1, i); % Power in watts
efficiency = Specs(2, i); % Efficiency in percentage
efficiency_decimal = efficiency / 100;
output_power = efficiency_decimal * power;
% Energy required to lift mass
energy_required = mass * g * height;
% Time to lift
time = energy_required / output_power;
% Store results: power, efficiency, mass, height, time
results(:, i) = [power; efficiency; mass; height; time];
end
% Export results to CSV file
csvwrite('Task2_Results.csv', results);
% Display the matrix to verify
disp('Results matrix:');
disp(results);
%Task 3
output_powers = Specs(1, :) .* (Specs(2, :) / 100); % Output power for each motor
times = results(5, :); % Time from the results matrix
scatter(output_powers, times, 'filled');
xlabel('Output Power (W)');
ylabel('Time to Lift Load (s)');
title('Motor Output Power vs. Time to Lift Load');
grid on;
  13 Comments
Sebastian
Sebastian on 17 Sep 2024
This is just the prompt. The matlab code grader is through the Canvas software
Cris LaPierre
Cris LaPierre on 18 Sep 2024
The prompt is helpful, but it would also be helpful to see the assessment page as well.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 17 Sep 2024
Names only has 4 strings in it, not 5 so that's why you get the error. The code is not very robust. Here, I've improved it by giving a better error message if the user enters the wrong number (see the while loop). It works for me in desktop MATLAB.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
load('Motor_Data.mat');
motor_index = inf; % Initialize.
while motor_index >= numel(Names) || motor_index < 1
fprintf('Available motors:\n');
for k = 1:length(Names)
fprintf('%d: %s\n', k, Names(k));
end
motor_index = input('Select a motor by entering its number: ');
if motor_index > numel(Names) || motor_index < 1
beep;
fprintf('NO SUCH MOTOR NUMBER! Enter a number between 1 and %d.\n', numel(Names));
end
end
mass = input('Enter the mass to be lifted (in kg): ');
height = input('Enter the height to lift the mass (in meters): ');
motor_name = Names(motor_index);
power = Specs(1, motor_index);
efficiency = Specs(2, motor_index);
% Calculate output power
efficiency_decimal = efficiency / 100;
output_power = efficiency_decimal * power;
g = 9.81;
energy_required = mass * g * height;
time = energy_required / output_power;
fprintf('Motor: %s\n', motor_name);
fprintf('Load lifted: %.2f kg\n', mass);
fprintf('Height lifted: %.2f meters\n', height);
fprintf('Time to lift the load: %.2f seconds\n', time);
%Task 2
mass = input('Enter the mass to be lifted by all motors (in kg): ');
height = input('Enter the height to lift the mass by all motors (in meters): ');
% Initialize results matrix
num_motors = length(Names);
results = zeros(5, num_motors);
% Calculate time for each motor
g = 9.81; % Gravity constant
for k = 1:num_motors
power = Specs(1, k); % Power in watts
efficiency = Specs(2, k); % Efficiency in percentage
efficiency_decimal = efficiency / 100;
output_power = efficiency_decimal * power;
% Energy required to lift mass
energy_required = mass * g * height;
% Time to lift
time = energy_required / output_power;
% Store results: power, efficiency, mass, height, time
results(:, k) = [power; efficiency; mass; height; time];
end
% Export results to CSV file
csvwrite('Task2_Results.csv', results);
% Display the matrix to verify
disp('Results matrix:');
disp(results);
%Task 3
output_powers = Specs(1, :) .* (Specs(2, :) / 100); % Output power for each motor
times = results(5, :); % Time from the results matrix
scatter(output_powers, times, 'filled');
xlabel('Output Power (W)');
ylabel('Time to Lift Load (s)');
title('Motor Output Power vs. Time to Lift Load');
grid on;

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!