MATLAB中使用日期字符串数组设置X轴刻度标签绘图的方法及报错解决
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:
Fix the date array
First, definexDatacorrectly 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'});Plot and set custom X-axis ticks
Instead of passing the string array directly toplot()(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

