Clear Filters
Clear Filters

i would like to know why my code doesn't work please, the data load factors is supposed to be taken from a .txt file from DASL website

83 views (last 30 days)
function load_factors_analysis()
% Load the data (assuming 'load_factors.csv' is in the working directory)
data = load('load_factors.csv');
% Get central and dispersion measurements for each variable
[mean, median, stddev, min_val, max_val] = central_dispersion_measurements(data);
% Create histograms and/or bar graphs for each variable
histograms_and_bar_graphs(data);
% Create a boxplot (assuming data(:,1) is continuous and data(:,2) is categorical)
boxplot_categorical(data(:, 1), data(:, 2));
% Perform a regression analysis (assuming data(:,1) and data(:,2) are continuous)
regression_analysis(data(:, 1), data(:, 2));
% Discuss the results (replace with your actual analysis)
discussion(data, mean, median, stddev, min_val, max_val);
end
function [mean, median, stddev, min_val, max_val] = central_dispersion_measurements(data)
% Get central and dispersion statistics
mean = mean(data);
median = median(data);
stddev = std(data);
min_val = min(data);
max_val = max(data);
% Return the calculated values
return (mean, median; stddev; min_val; max_val);
end
function histograms_and_bar_graphs(data)
for i = 1:size(data, 2)
if isnumeric(data(:, i))
h = histogram(data(:, i));
title(strcat('Histogram of ', data(1, i)));
show(h);
else
b = bar(unique(data(:, i)), count(data(:, i)));
title(strcat('Bar graph of ', data(1, i)));
show(b);
end
end
end
function boxplot_categorical(data_continuous, data_categorical)
% Create a boxplot for continuous data divided by categorical classes
boxplot(data_continuous, data_categorical);
end
function regression_analysis(data1, data2)
% Perform linear regression
[b, a, rsq, pval, se] = regress(data1, data2);
% Print the results
disp('Coefficients:');
disp([b, a]);
disp('R-squared:');
disp(rsq);
disp('P-value:');
disp(pval);
disp('Standard error:');
disp(se);
end
function discussion(~, ~, ~, ~, ~, ~)
% Replace this with your analysis of the data and calculated statistics
disp('This is a placeholder for your data analysis discussion.');
disp('Here, you would discuss insights from the central tendency');
disp('(mean, median), dispersion (standard deviation),');
disp('minimum and maximum values, and any relationships found');
disp('between variables using the boxplot and regression analysis.');
end

Accepted Answer

Voss
Voss on 29 Jun 2024 at 15:51
There are a few problems that I see:
1. In MATLAB, return doesn't take arguments, so you don't say
return (mean, median; stddev; min_val; max_val);
You just say
return
and the vaues that are returned from the function (i.e., the output arguments of the function) are defined in the function definition, as you are doing
function [mean, median, stddev, min_val, max_val] = central_dispersion_measurements(data)
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ outputs
However, in this case, since the return statement is at the end of the function, you don't need it at all. You can remove it entirely.
2. Don't use mean and median as variable names, since they are names of built-in functions that you need to use. Replace those variable names with other names that don't conflict with built-in MATLAB functions, such as mean_data and median_data, for example.
3. The show function is for displaying information about an optimization object, not for making graphics show up or whatever you intended. You don't need to do anything special to make graphics show up, just create them (e.g., using histogram or bar, like you do here). Remove the show lines.
4. For robustness (i.e., correctly handling the case that data has only one row), you should specify that you want to take the mean, median, etc., over the rows of data by specifying dimension 1 in those functions. That is:
mean_data = mean(data,1);
median_data = median(data,1);
stddev = std(data,0,1);
min_val = min(data,[],1);
max_val = max(data,[],1);
There may be other problems. If you run into other problems, upload your data file here (using the paperclip button).
  17 Comments
Voss
Voss on 7 Jul 2024 at 16:42
@Ahmed: The code generates histograms and boxplots when I run it:
data = readmatrix('load_factors_2019.txt');
% Get central and dispersion measurements for each variable
[mean, median, stddev, min_val, max_val] = central_dispersion_measurements(data);
% Create histograms and/or bar graphs for each variable
histograms_and_bar_graphs(data);
Warning: Input should be a string, character array, or cell array of character arrays.
% Create a boxplot (assuming data(:,1) is continuous and data(:,2) is categorical)
boxplot_categorical(data(:, 1), data(:, 2));
% Perform a regression analysis (assuming data(:,1) and data(:,2) are continuous)
regression_analysis(data(:, 1), data(:, 2));
Warning: R-square and the F statistic are not well-defined unless X has a column of ones.
Type "help regress" for more information.
Coefficients: 241.2692 223.5298 259.0086 R-squared: 1.0e+03 * -0.4107 -0.6520 -0.8932 1.7617 1.5205 1.2792 1.0379 0.7967 0.5554 0.3141 0.0728 -0.1684 -0.4097 -0.6510 -0.8922 1.7627 1.5215 1.2802 1.0389 0.7977 0.5564 0.3151 0.0738 -0.1674 -0.4087 -0.6500 -0.8912 1.7637 1.5225 1.2812 1.0399 0.7987 0.5574 0.3161 0.0748 -0.1664 -0.4077 -0.6490 -0.8902 1.7647 1.5235 1.2822 1.0409 0.7997 0.5584 0.3171 0.0758 -0.1654 -0.4067 -0.6480 -0.8892 1.7657 1.5245 1.2832 1.0419 0.8007 0.5594 0.3181 0.0768 -0.1644 -0.4057 -0.6470 -0.8882 1.7667 1.5255 1.2842 1.0429 0.8017 0.5604 0.3191 0.0778 -0.1634 -0.4047 -0.6460 -0.8872 1.7677 1.5265 1.2852 1.0439 0.8027 0.5614 0.3201 0.0788 -0.1624 -0.4037 -0.6450 -0.8862 1.7687 1.5275 1.2862 1.0449 0.8037 0.5624 0.3211 0.0798 -0.1614 -0.4027 -0.6440 -0.8852 1.7697 1.5285 1.2872 1.0459 0.8047 0.5634 0.3221 0.0808 -0.1604 -0.4017 -0.6430 -0.8842 1.7707 1.5295 1.2882 1.0469 0.8057 0.5644 0.3231 0.0818 -0.1594 -0.4007 -0.6420 -0.8832 1.7717 1.5305 1.2892 1.0479 0.8067 0.5654 0.3241 0.0828 -0.1584 -0.3997 -0.6410 -0.8822 1.7727 1.5315 1.2902 1.0489 0.8077 0.5664 0.3251 0.0838 -0.1574 -0.3987 -0.6400 -0.8812 1.7737 1.5325 1.2912 1.0499 0.8087 0.5674 0.3261 0.0848 -0.1564 -0.3977 -0.6390 -0.8802 1.7747 1.5335 1.2922 1.0509 0.8097 0.5684 0.3271 0.0858 -0.1554 -0.3967 -0.6380 -0.8792 1.7757 1.5345 1.2932 1.0519 0.8107 0.5694 0.3281 0.0868 -0.1544 -0.3957 -0.6370 -0.8782 1.7767 1.5355 1.2942 1.0529 0.8117 0.5704 0.3291 0.0878 -0.1534 -0.3947 -0.6360 -0.8772 1.7777 1.5365 1.2952 1.0539 0.8127 0.5714 0.3301 0.0888 -0.1524 P-value: 1.0e+03 * -2.2708 1.4494 -2.5089 1.2050 -2.7463 0.9598 -0.0915 3.6150 -0.3367 3.3776 -0.5810 3.1394 -0.8246 2.9004 -1.0674 2.6607 -1.3094 2.4202 -1.5506 2.1789 -1.7911 1.9368 -2.0308 1.6940 -2.2698 1.4504 -2.5079 1.2060 -2.7453 0.9608 -0.0905 3.6160 -0.3357 3.3786 -0.5800 3.1404 -0.8236 2.9014 -1.0664 2.6617 -1.3084 2.4212 -1.5496 2.1799 -1.7901 1.9378 -2.0298 1.6950 -2.2688 1.4514 -2.5069 1.2070 -2.7443 0.9618 -0.0895 3.6170 -0.3346 3.3796 -0.5790 3.1414 -0.8226 2.9024 -1.0654 2.6627 -1.3074 2.4221 -1.5486 2.1809 -1.7891 1.9388 -2.0288 1.6960 -2.2678 1.4524 -2.5059 1.2080 -2.7433 0.9628 -0.0885 3.6180 -0.3336 3.3805 -0.5780 3.1424 -0.8215 2.9034 -1.0643 2.6637 -1.3064 2.4231 -1.5476 2.1819 -1.7881 1.9398 -2.0278 1.6970 -2.2668 1.4534 -2.5049 1.2090 -2.7423 0.9639 -0.0875 3.6189 -0.3326 3.3815 -0.5770 3.1433 -0.8205 2.9044 -1.0633 2.6646 -1.3054 2.4241 -1.5466 2.1829 -1.7871 1.9408 -2.0268 1.6980 -2.2658 1.4544 -2.5039 1.2100 -2.7413 0.9649 -0.0865 3.6199 -0.3316 3.3825 -0.5759 3.1443 -0.8195 2.9054 -1.0623 2.6656 -1.3044 2.4251 -1.5456 2.1839 -1.7861 1.9418 -2.0258 1.6990 -2.2648 1.4554 -2.5029 1.2110 -2.7403 0.9659 -0.0854 3.6209 -0.3306 3.3835 -0.5749 3.1453 -0.8185 2.9064 -1.0613 2.6666 -1.3034 2.4261 -1.5446 2.1848 -1.7851 1.9428 -2.0248 1.7000 -2.2638 1.4564 -2.5020 1.2120 -2.7393 0.9669 -0.0844 3.6219 -0.3296 3.3845 -0.5739 3.1463 -0.8175 2.9073 -1.0603 2.6676 -1.3023 2.4271 -1.5436 2.1858 -1.7841 1.9438 -2.0238 1.7010 -2.2628 1.4574 -2.5010 1.2130 -2.7384 0.9679 -0.0834 3.6229 -0.3285 3.3855 -0.5729 3.1473 -0.8165 2.9083 -1.0593 2.6686 -1.3013 2.4281 -1.5426 2.1868 -1.7831 1.9448 -2.0228 1.7020 -2.2618 1.4584 -2.5000 1.2140 -2.7374 0.9689 -0.0824 3.6238 -0.3275 3.3865 -0.5719 3.1483 -0.8155 2.9093 -1.0583 2.6696 -1.3003 2.4291 -1.5416 2.1878 -1.7821 1.9458 -2.0218 1.7030 -2.2608 1.4594 -2.4990 1.2151 -2.7364 0.9699 -0.0814 3.6248 -0.3265 3.3874 -0.5709 3.1493 -0.8145 2.9103 -1.0573 2.6706 -1.2993 2.4301 -1.5406 2.1888 -1.7811 1.9468 -2.0208 1.7040 -2.2598 1.4604 -2.4980 1.2161 -2.7354 0.9709 -0.0803 3.6258 -0.3255 3.3884 -0.5699 3.1503 -0.8135 2.9113 -1.0563 2.6716 -1.2983 2.4311 -1.5396 2.1898 -1.7801 1.9478 -2.0198 1.7050 -2.2588 1.4614 -2.4970 1.2171 -2.7344 0.9719 -0.0793 3.6268 -0.3245 3.3894 -0.5689 3.1512 -0.8124 2.9123 -1.0553 2.6726 -1.2973 2.4321 -1.5386 2.1908 -1.7791 1.9488 -2.0188 1.7060 -2.2578 1.4624 -2.4960 1.2181 -2.7334 0.9729 -0.0783 3.6278 -0.3235 3.3904 -0.5678 3.1522 -0.8114 2.9133 -1.0543 2.6736 -1.2963 2.4331 -1.5376 2.1918 -1.7781 1.9498 -2.0178 1.7070 -2.2568 1.4634 -2.4950 1.2191 -2.7324 0.9740 -0.0773 3.6288 -0.3224 3.3914 -0.5668 3.1532 -0.8104 2.9143 -1.0533 2.6746 -1.2953 2.4341 -1.5366 2.1928 -1.7771 1.9508 -2.0168 1.7080 -2.2558 1.4644 -2.4940 1.2201 -2.7314 0.9750 -0.0763 3.6297 -0.3214 3.3924 -0.5658 3.1542 -0.8094 2.9153 -1.0522 2.6756 -1.2943 2.4351 -1.5356 2.1938 -1.7761 1.9518 -2.0158 1.7090 -2.2548 1.4654 -2.4930 1.2211 -2.7304 0.9760 -0.0753 3.6307 -0.3204 3.3933 -0.5648 3.1552 -0.8084 2.9163 -1.0512 2.6765 -1.2933 2.4361 -1.5346 2.1948 -1.7751 1.9528 -2.0149 1.7100 Standard error: 1.0e+05 * -0.3680 NaN NaN 8.9444
% Discuss the results (replace with your actual analysis)
discussion(data, mean, median, stddev, min_val, max_val);
This is a placeholder for your data analysis discussion. Here, you would discuss insights from the central tendency (mean, median), dispersion (standard deviation), minimum and maximum values, and any relationships found between variables using the boxplot and regression analysis.
function [mean_data, median_data, stddev, min_val, max_val] = central_dispersion_measurements(data)
% Get central and dispersion statistics
mean_data = mean(data,1);
median_data = median(data,1);
stddev = std(data,0,1);
min_val = min(data,[],1);
max_val = max(data,[],1);
end
function histograms_and_bar_graphs(data)
for i = 1:size(data, 2)
if isnumeric(data(:, i))
histogram(data(:, i));
title(strcat('Histogram of ', data(1, i)));
else
bar(unique(data(:, i)), count(data(:, i)));
title(strcat('Bar graph of ', data(1, i)));
end
end
end
function boxplot_categorical(data_continuous, data_categorical)
% Create a boxplot for continuous data divided by categorical classes
boxplot(data_continuous, data_categorical);
end
function regression_analysis(data1, data2)
% Perform linear regression
[b, a, rsq, pval, se] = regress(data1, data2);
% Print the results
disp('Coefficients:');
disp([b, a]);
disp('R-squared:');
disp(rsq);
disp('P-value:');
disp(pval);
disp('Standard error:');
disp(se);
end
function discussion(~, ~, ~, ~, ~, ~)
% Replace this with your analysis of the data and calculated statistics
disp('This is a placeholder for your data analysis discussion.');
disp('Here, you would discuss insights from the central tendency');
disp('(mean, median), dispersion (standard deviation),');
disp('minimum and maximum values, and any relationships found');
disp('between variables using the boxplot and regression analysis.');
end

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!