- Add the From Workspace block to your model and connect it.
- In the parameters for the From Workspace block, specify the name of the variable you'll use for the signal data. For example, voltageData.
- In the MATLAB Command Window, create the variable for the From Workspace block to load using the data you loaded into MATLAB from your txt file.
Importing Voltage Data to AC Voltage Source in Simulink
37 次查看（过去 30 天）
I would like to import voltage data from text in matlab and then I would like to use this data in simulink AC voltage source .I have written one code but not sure is it okay or not as it is not
% Import voltage data from text file
data = importdata('voltage_data.txt');
time = data.data(:, 1); % time data in seconds
voltage = data.data(:, 2); % voltage data in volts
mdl = 'ac_source_model';
set_param(mdl, 'Solver', 'ode23');
% Add AC voltage source to model
add_block('Simscape / Electrical / Specialized Power Systems / Sources/AC Voltage Source', [mdl '/Voltage Source']);
set_param([mdl '/Voltage Source'], 'Amplitude', 'voltage');
set_param([mdl '/Voltage Source'], 'Frequency', '50');
set_param([mdl '/Voltage Source'], 'Phase', '0');
% Run simulation
It can't find the block from simulink using this code.Is this way is okay or is there any other way I can use variable text data in simulink(As Voltage Source). N:B:I have read that there is a block called From Workspace which can also be used for this purpose
Sara Nadeau 2023-4-24
I believe you'll need to load the data from the workspace into Simulink using a loading block that produces the signal that would be the input for the Voltage Source block. The From Workspace block is one of the loading blocks you could use.
It's not clear to me exactly what your data looks like based on the code you showed in your answer. To load data into Simulink, I believe you'll need to remove any non-numeric text from each value. For example, if your text file captures the time as "1 s" including "s" for "s", you'd need to get rid of that "s" part. If your file contains only numbers in each column, then you don't need to worry about this.
Assuming your data includes only numbers in each column, here's the basic process for loading data using the From Workspace block:
For step 3, you'll need to create the variable using a format that the From Workspace block supports. The simplest format is array:
voltageData = [time voltage];
Array format has several limitations and stipluations, though, including that the values must be double. In general, timeseries format has better support.
voltageData = timeseries(voltage,time);
Here's a page that shows example code for each format the From Workspace block supports: https://www.mathworks.com/help/simulink/ug/load-data-using-the-from-workspace-block.html
Here's the reference page for the From Workspace block, which provides more information about its usage: https://www.mathworks.com/help/simulink/slref/fromworkspace.html