Error in close calculation and plotting

28 views (last 30 days)
Nikita
Nikita on 16 Jan 2026 at 20:52
Commented: Nikita ungefär 3 timmar ago
Hey,
I have been using this code and it has worked really well in the past. It is giving me an error now. I'm attaching the file, pasting the code and the linked error. It would be great to have some help!
% detrended correlated Rs>0.7
[UClass,~,idx] = unique(detrend_correl_07_slope_1.id,'stable');
XYc = accumarray(idx, (1:numel(idx)).', [], @(x){detrend_correl_07_slope_1(x,:)})
XYc{3}
for k = 1:numel(XYc)
B(:,k) = [XYc{k}{:,3} ones(size(XYc{k}{:,3}))] \ XYc{k}{:,4};
detrend_correl_07_slope_1(k) = B(1,k);
end
slope_ln_data_11
slope_ln_data_11 = slope_ln_data_11'
figure
tiledlayout(numel(XYc),1)
for k = 1:numel(XYc)
nexttile
plot(XYc{k}{:,3}, XYc{k}{:,4}, 'p')
hold on
L = [XYc{k}{:,3} ones(size(XYc{k}{:,3}))] * B(:,k);
plot(XYc{k}{:,3}, L, '-r')
hold off
grid
axis('padded')
title("Slope = "+slope_ln_data_11(k))
end
It's throwing up the following error for this line of code
detrend_correl_07_slope_1(k) = B(1,k);
Error using () (line 115)
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row
subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for
one variable t.(i). To select rows, use t(i,:).

Accepted Answer

dpb
dpb on 16 Jan 2026 at 21:27
That code could never have worked as is if detrend_correl_07_slope_1 was a table. You used the .id variable dot referencing earlier in the call to unique, you need the same here...the dot has somehow gotten lost. Use
detrend_correl_07_slope_1.(k) = B(1,k);
to reference each column k in turn.

More Answers (1)

Umar
Umar on 17 Jan 2026 at 6:01
Hi @Nikita,I've identified the issue with your code. The error occurs because detrend_correl_07_slope_1 is a table, not a regular array. MATLAB tables cannot be indexed with single subscripts like detrend_correl_07_slope_1(k). They require two subscripts for rows and columns, or dot notation to access variables. The problematic line is: detrend_correl_07_slope_1(k) = B(1,k); This worked previously because your data was likely imported as a numeric array rather than a table. Something has changed in how the data is being loaded or the MATLAB version you're using. Here's the fix. Replace that line with this approach: slope_ln_data_11 = zeros(numel(XYc), 1); for k = 1:numel(XYc) B(:,k) = [XYc{k}{:,3} ones(size(XYc{k}{:,3}))] \ XYc{k}{:,4}; slope_ln_data_11(k) = B(1,k); end Then if you need to add these slopes back to your table later, you can use: detrend_correl_07_slope_1.slope = slope_ln_data_11'; This keeps your slope calculations in a regular numeric array where single-index subscripting works perfectly. The rest of your code should run without any changes. According to the MathWorks documentation, tables require explicit row and column specification. You can access table data using table(rows, vars) syntax, curly braces table{rows, vars} to extract the underlying array, or dot notation table.VariableName for specific columns. Let me know if you run into any other issues with this fix.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!