Clear Filters
Clear Filters

Error in boxchart (invalid parameter/value pair arguments)

25 views (last 30 days)
I am trying to use Boxchart but am getting an error even when using the carbig dataset and functions as listed in the help files. Eventually I need to get boxcharts for ANOVA results. This is what I have. (The help file for anovan calls "Model_Year" as "mfg date" but I think this is the equivalent set of data)
aov = anovan(MPG,{org when},'model',2,'varnames',{'Origin','Model_Year'})
boxchart(aov,["Origin"])
legend
The ANOVA seems to run just fine, but when it gets to the Boxchart I get this error. Any ideas? I'm using version R2023b.
Error using matlab.graphics.chart.primitive.BoxChart
Invalid parameter/value pair arguments.
Error in boxchart (line 186)
H(idx) = matlab.graphics.chart.primitive.BoxChart('Parent', cax,...
Error in ANOVA_trial_file (line 2)
boxchart(p,["Origin"])

Accepted Answer

Umar
Umar on 12 Aug 2024 at 20:41

Hi @Laura ,

To address your issue with the `boxchart` function after running ANOVA with the `anovan` function, let me break down the process and identify potential causes for the error you're encountering.When you run:

aov = anovan(MPG, {org, when}, 'model', 2, 'varnames', {'Origin', 'Model_Year'});

You are correctly obtaining p-values and other outputs from the ANOVA analysis. The output variable `aov` contains statistical results but not in a format directly usable by `boxchart`. The `boxchart` function is designed to create box plots based on categorical data. To use it effectively, you need to ensure that you're passing appropriate data structures to it. The error message suggests that the parameters you are providing to `boxchart` do not align with its expected input. Instead of passing the output of `anovan` directly to `boxchart`, you should extract your group variable (in this case, 'Origin') and the corresponding data (MPG) separately. Here’s how you can structure your code:

    % Assuming carbig dataset is already loaded
    load carbig;
    % Perform ANOVA
    [p, tbl, stats] = 
    anovan(MPG, {org, when}, 'model', 2, 'varnames', {'Origin', 'Model_Year'});
    % Create a box chart for MPG based on Origin
     boxchart(categorical(org), MPG);
     legend('Origin');

So, as you can see, I use ‘categorical(org)` to convert your grouping variable into a categorical format suitable for box charts and directly use `MPG` as the response variable for plotting.

I also noticed that you are using R2023b, make that your MATLAB installation is up-to-date. Occasionally, functions may behave differently across versions due to updates or bug fixes. Also, before plotting, it’s good practice to check if both `MPG` and `org` contain valid data without missing values or inconsistencies. You can use:

disp(sum(isnan(MPG))); % Check for NaN values in MPG
disp(unique(org));      % View unique values in org

Now, you should be able to successfully create your box chart without encountering parameter errors. If issues persist, double-check the inputs and consult MATLAB documentation for any changes specific to your version. Please let me know if you still have any further questions.

  2 Comments
Laura
Laura on 13 Aug 2024 at 13:59
Thank you, I was able to use this to figure it out! I had to additionally use cellstr to convert my data before it would let me use categorical, but I got it. I really appreciate the very clearly explained answer.
Umar
Umar on 13 Aug 2024 at 23:21
Hi @Laura,
No problem. Glad to help out. Also, please don’t forget to click “Accept Answer”. If you still encounter any problems, please don’t hesitate to ask for help, we will be more happy to help you out.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 12 Aug 2024 at 20:42
anovan returns a vector of p values, not an anova object.
Your syntax would have been okay if you had called anova

Community Treasure Hunt

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

Start Hunting!