You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB中使用日期字符串数组设置X轴刻度标签绘图的方法及报错解决

Fixing the "Not enough input arguments" Error for Date-Based Plotting in MATLAB

Let's work through this issue step by step—first, let's figure out why you're getting that error, then build the correct plot with your date labels.

Why the Error Happens

The root problem is how you defined your xData array. In MATLAB, when creating a string array, you need to separate individual elements with commas. Your code ["01/22" "01/23"...] doesn't use commas, so MATLAB treats the whole thing as a single concatenated string instead of an array of 8 separate dates. When you pass this single string to plot() alongside your 8-element yData, MATLAB can't resolve the mismatched inputs, hence the "Not enough input arguments" error.

Correct Implementation

Here's how to properly create your plot with the date labels as X-axis ticks:

  1. Fix the date array
    First, define xData correctly as a MATLAB string array (note the commas between elements):

    xData = ["01/22", "01/23", "01/24", "01/25", "01/26", "01/27", "01/28", "01/29"];
    yData = [557 655 941 1433 2118 2927 5578 6167];
    

    Alternatively, you can create it from a cell array of characters:

    xData = string({'01/22','01/23','01/24','01/25','01/26','01/27','01/28','01/29'});
    
  2. Plot and set custom X-axis ticks
    Instead of passing the string array directly to plot() (which can cause unexpected behavior with discrete labels), use numerical indices as the X-values, then replace the tick labels with your dates:

    % Plot the data using indices (1 to 8) as X coordinates
    plot(1:length(xData), yData, '-o'); % The '-o' adds markers for clarity (optional)
    
    % Replace X-axis ticks with your date labels
    ax = gca; % Get the current axis handle
    ax.XTick = 1:length(xData); % Set ticks to match each data point
    ax.XTickLabel = xData; % Assign your date strings to the ticks
    
    % Optional: Add labels and title to make the plot clearer
    title('Daily Value Trend');
    xlabel('Date (mm/dd)');
    ylabel('Recorded Value');
    

This approach ensures each data point lines up perfectly with its corresponding date label, and avoids the input mismatch that caused your original error.

内容的提问来源于stack exchange,提问作者livemyaerodream

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:08:13