필터 지우기
필터 지우기

Need help with Matlab homework

조회 수: 2 (최근 30일)
Jeff
Jeff 2023년 12월 9일
댓글: Jeff 2023년 12월 10일
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
Dyuman Joshi
Dyuman Joshi 2023년 12월 9일
편집: Dyuman Joshi 2023년 12월 9일
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 2023년 12월 9일
thank you for responding, I have attached the zip file with the inputs.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 12월 9일
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
Jeff
Jeff 2023년 12월 10일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by