Effacer les filtres
Effacer les filtres

Need help with Matlab homework

2 vues (au cours des 30 derniers jours)
Jeff
Jeff le 9 Déc 2023
Commenté : Jeff le 10 Déc 2023
Hi, I am finding it difficult to fix errors on my homework, if anyone can help with changes I can add to my code for it to run properly
%clear workspace and command window
clear; clc
% MATLAB Inventory Management System
% User Interaction: Get user input for the number of products
numProducts = input('Enter the number of products: ');
% User Interaction: Get product names using listdlg
productNames = listdlg('PromptString', 'Select product(s):', 'ListString', {'Cerevita 500g', 'Cremora 1kg', 'Matemba 500g'}, 'SelectionMode', 'multiple');
% Read data from a file
data = readmatrix("products inventory.xlsx");
% Calculate total value of selected products
totalValue = sum(data, productNames);
% Perform statistical analysis on product prices
stats = statistical_analysis(data);
% Display product information
product_information(productNames, totalValue, stats);
% Plotting: Bar chart of total values for selected products
figure;
bar(productNames, totalValue);
xlabel('Selected Products');
ylabel('Total Value');
title('Total Value of Selected Products');
% Loop Statement: Update prices for all products
priceIncrease = 0.05; % 5% price increase
data = data * (1 + priceIncrease);
% Save updated data to a new file
writematrix('updated_products_inventory.xlsx', data);
fprintf('\nUpdated data saved to "updated_products_inventory.xlsx".\n');
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 9 Déc 2023
Modifié(e) : Dyuman Joshi le 9 Déc 2023
Without inputs such as numProducts, productNames and the files used, we can not run your code and are unable to reproduce the error you get.
Nor do we know which error do you get and where does it occurs.
So, provide all the information that will help us help you.
Jeff
Jeff le 9 Déc 2023
thank you for responding, I have attached the zip file with the inputs.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 9 Déc 2023
data = readmatrix("products inventory.xlsx");
You used readmatrix() to read from a .xlsx file. When readmatrix() works, the output is an array with as many columns as there were columns in the file, and where the whole output array is the same datatype. For example the entire array might be double. Or it might be a cell array of character vectors.
When you use readmatrix(), all of the columns must be the same datatype, or else readmatrix() will either fail or else will convert values (for example might convert text into NaN values.)
Because an array is returned, the individual columns do not retain any identifying information. You might know that the second column corresponds to 'Cremora 1kg' but the output of readmatrix() will not know that.
If you had used readtable() instead of readmatrix(), then readtable() would attempt to deduce column names from the file and to use the column names as the names of variables in the table.
% Calculate total value of selected products
totalValue = sum(data, productNames);
The output of the inputdlg() that was assigned to productNames, is a numeric vector that indicates which entries were selected.
When you pass a numeric value as the second parameter to sum(), then the numeric values are assumed to be the indices of the dimensions to sum over. Passing in productNames into sum() that way does not select columns from data
You could potentially select columns from data by using
totalValue = sum(data(:,productNames),1)
which would produce a total per column.
  1 commentaire
Jeff
Jeff le 10 Déc 2023
I have attached the scripts, I am still receiving errors. If you could please check my code for errors so I can fix it.
Thank you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical 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