Set a certain bit to a 1 matlab - matlab

I want to set a certain bit of my configuration header to a 1 (bit 3 of Byte 10) if its not already a 1. And then I want to save this new config header as my old one. I can set the bit ok, however I'm having difficulty replacing the old byte with my new to update my configuration header. I want my new configuration header to be ConfigHeader = [128;0;0;0;0;0;0;0;0;0;30];. I know it's probably a very simple solution!!
ConfigHeader = [128;0;0;0;0;0;0;0;0;0;26];
ByteTen = ConfigHeader(11);
if bitget(ByteTen,3) == 0
% Set bit 3 of byte 10 to 1
bitset(ByteTen,3);
% Replace old config header with new one (bit changed to 1)
ConfigHeader = ??????
end

You'll have to assign the modified value back to ConfigHeader, e.g. by adding this line:
ConfigHeader(11) = bitset(ByteTen,3);
This line:
bitset(ByteTen,3);
on it's own does nothing as long as you don't use its output.

I am not used to bit operations, but juding from your code the next logical step would be:
ConfigHeader(11) = ByteTen
Perhaps your entire code can even be reduced to:
bitset(ConfigHeader(11),3)
After all, a bit can only be zero or one so you don't need to test it if you always want to end with a one.

Related

LO Calc Basic - What is the right property name for the axis major/minor settings and how to set them correctly?

Using a libreoffice basic macro for charts, we can control the maximum value of an axis and turn the automatic mode on/off:
oAxis.AutoMax = False
oAxis.Max = 12345
But what are the right property names for
Major Interval
Major Auto
Major Time
which you can set manually
???
First, I created a chart with Insert Chart > Line > Points and Lines.
Modifying the Y axis with code was fairly straightforward.
For both axes, I went into formatting and specified Positioning > Interval Marks > Minor > Outer so that the minor ticks are shown.
oCharts = ThisComponent.getSheets().getByIndex(0).getCharts()
oEmbeddedObject = oCharts.getByIndex(0).getEmbeddedObject()
oDiagram = oEmbeddedObject.getDiagram()
oYAxis = oDiagram.getYAxis()
oYAxis.StepMain = 40.0
oYAxis.StepHelpCount = 3
Here is what the Y Axis properties looked like after running the code:
AutoStepMain (and the corresponding Major interval checkbox) started out as True, but setting the StepMain value via macro changed it to False.
With the X axis, things were more complex. For the scale, there was a choice of Type, and selecting Date seemed to be the only way to control step settings.
After that, instead of StepMain (which didn't seem to be relevant in this case), there is a complex structure called ExplicitTimeIncrement that specifies the type of increment (Days or Months) along with each value. I didn't dig too far into it, but it looks like it has all of the values you were asking about.
EDIT:
I tried the following code, but none of the values were changed, and the last line throws an error stating that the property is read-only (as also shown by MRI). So perhaps the values cannot be modified via the API.
sTimeIntervalMajor = CreateUnoStruct("com.sun.star.chart.TimeInterval")
sTimeIntervalMajor.Number = 4
sTimeIntervalMajor.TimeUnit = 0
sTimeIntervalMinor = CreateUnoStruct("com.sun.star.chart.TimeInterval")
sTimeIntervalMinor.Number = 1
sTimeIntervalMinor.TimeUnit = 0
sTimeIncrement = CreateUnoStruct("com.sun.star.chart.TimeIncrement")
sTimeIncrement.MajorTimeInterval = sTimeIntervalMajor
sTimeIncrement.MinorTimeInterval = sTimeIntervalMinor
sTimeIncrement.TimeResolution = 1
oXAxis = oDiagram.getXAxis()
oXAxis.ExplicitTimeIncrement.MajorTimeInterval = sTimeIntervalMajor
oXAxis.setPropertyValue("ExplicitTimeIncrement", sTimeIncrement)
oXAxis.ExplicitTimeIncrement = sTimeIncrement
It might also be worth posting at ask.libreoffice.org or forum.openoffice.org to see if anyone there can find a way to modify the values, with a link to this question.
Of course, the UNO API isn't the only possibility. You could write a script to unzip the .ods file and modify the XML code with a parsing library such as xml.etree or regular expressions.

Line that loads based on the value of a variable in Flutter

I try to explain to you as best as possible what I need. I would like to create a line with the style similar to the loading ones like this:
and to be able to decide the initial and final value (for example start at 0 and end at 100) and advance the line according to the value of an int variable.
So if for example I create a line from 0 to 100 and put the value of the variable at 50 it should be half full.

matlab: check which lines of a path are used - graphshortestpath

The related problem comes from the power Grid in Germany. I have a network of substations, which are connected according to the Lines. The shortest way from point A to B was calculated using the graphshortestpath function. The result is a path with the used substation ID's. I am interested in the Line ID's though, so I have written a sequential code to figure out the used Line_ID's for each path.
This algorithm uses two for loops. The first for-loop to access the path from a cell array, the second for-loop looks at each connection and searches the Line_ID from the array.
Question: Is there a better way of coding this? I am looking for the Line_ID's, graphshortestpath only returns the node ID's.
Here is the main code:
for i = i_entries
path_i = LKzuLK_path{i_entries};
if length(path_i) > 3 %If length <=3 no lines are used.
id_vb = 2:length(path_i) - 2;
for id = id_vb
node_start = path_i(id);
node_end = path_i(id+1);
idx_line = find_line_idx(newlinks_vertices, node_start, ...
node_end);
Zuordnung_LKzuLK_pathLines(ind2sub(size_path,i),idx_line) = true;
end
end
end
Note: The first and last enrty of path_i are area ID's, so they are not looked upon for the search for the Line_ID's
function idx_line = find_line_idx(newlinks_vertices, v_id_1, v_id_2)
% newlinks_vertices includes the Line_ID, and then the two connecting substations
% Mirror v_id's in newlinks_vertices:
check_links = [newlinks_vertices; newlinks_vertices(:,1), newlinks_vertices(:,3), newlinks_vertices(:,2)];
tmp_dist1 = find(check_links(:,2) == v_id_1);
tmp_dist2 = find(check_links(tmp_dist1,3) == v_id_2,1);
tmp_dist3 = tmp_dist1(tmp_dist2);
idx_line = check_links(tmp_dist3,1);
end
Note: I have already tried to shorten the first find-search routine, by indexing the links list. This step will return a short list with only relevant entries of the links looked upon. That way the algorithm is reduced of the first and most time consuming find function. The result wasn't much better, the calculation time was still at approximately 7 hours for 401*401 connections, so too long to implement.
I would look into Dijkstra's algorithm to get a faster implementation. This is what Matlab's graphshortestpath uses by default. The linked wiki page probably explains it better than I ever could and even lays it out in pseudocode!

Find a Name in an Email (Low-Level I/O)

Round 2: Picking out leaders in an email
Alrighty, so my next problem is trying to figure out who the leader is in a project. In order to determine this, we are given an email and have to find who says "Do you want..." (capitalization may vary). I feel like my code should work for the most part, but I really have an issue figuring out how to correctly populate my cell array. I can get it to create the cell array, but it just puts the email in it over over again. So each cell is basically the name.
function[Leader_Name] = teamPowerHolder(email)
email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line
conversations = {lines}; %// Creates my cell array
while ischar(lines) %// Populates my cell array, just not correct
Convo = fgets(email);
if Convo == -1 %// Prevents it from just logging -1 into my cell array like a jerk
break; %// Returns to function
end
conversations = [conversations {lines}]; %// Populates my list
end
Sentences = strfind(conversations,'Do you want'); %// Locates the leader position
Leader_Name = Sentences{1}; %// Indexes that position
fclose(email);
end
What I ideally need it to do is find the '/n' character (hence why I used fgets) but I'm not sure how to make it do that. I tried to have my while loop be like:
while lines == '/n'
but that's incorrect. I feel like I know how to do the '/n' bit, I just can't think of it. So I'd appreciate some hints or tips to do that. I could always try to strsplit or strtok the function, but I need to then populate my cell array so that might get messy.
Please and thanks for help :)
Test Case:
Anna: Hey guys, so I know that he just assigned this project, but I want to go ahead and get started on it.
Can you guys please respond and let me know a weekly meeting time that will work for you?
Wiley: Ummmmm no because ain't nobody got time for that.
John: Wiley? What kind of a name is that? .-.
Wiley: It's better than john. >.>
Anna: Hey boys, let's grow up and talk about a meeting time.
Do you want to have a weekly meeting, or not?
Wiley: I'll just skip all of them and not end up doing anything for the project anyway.
So I really don't care so much.
John: Yes, Anna, I'd like to have a weekly meeting.
Thank you for actually being a good teammate and doing this. :)
out2 = teamPowerHolder('teamPowerHolder_convo2.txt')
=> 'Anna'
The main reason why it isn't working is because you're supposed to update the lines variable in your loop, but you're creating a new variable called Convo that is updating instead. This is why every time you put lines in your cell array, it just puts in the first line repeatedly and never quits the loop.
However, what I would suggest you do is read in each line, then look for the : character, then extract the string up until the first time you encounter this character minus 1 because you don't want to include the actual : character itself. This will most likely correspond to the name of the person that is speaking. If we are missing this occurrence, then that person is still talking. As such, you would have to keep a variable that keeps track of who is still currently talking, until you find the "do you want" string. Whoever says this, we return the person who is currently talking, breaking out of the loop of course! To ensure that the line is case insensitive, you'll want to convert the string to lower.
There may be a case where no leader is found. In that case, you'll probably want to return the empty string. As such, initialize Leader_Name to the empty string. In this case, that would be []. That way, should we go through the e-mail and find no leader, MATLAB will return [].
The logic that you have is pretty much correct, but I wouldn't even bother storing stuff into a cell array. Just examine each line in your text file, and keep track of who is currently speaking until we encounter a sentence that has another : character. We can use strfind to facilitate this. However, one small caveat I'll mention is that if the person speaking includes a : in their conversation, then this method will break.
Judging from the conversation that I'm seeing your test case, this probably won't be the case so we're OK. As such, borrowing from your current code, simply do this:
function[Leader_Name] = teamPowerHolder(email)
Leader_Name = []; %// Initialize leader name to empty
name = [];
email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line
while ischar(lines)
% // Get a line in your e-mail
lines = fgets(email);
% // Quit like a boss if you see a -1
if lines == -1
break;
end
% // Check if this line has a ':' character.
% // If we do, then another person is talking.
% // Extract the characters just before the first ':' character
% // as we don't want the ':' character in the name
% // If we don't encounter a ':' character, then the same person is
% // talking so don't change the current name
idxs = strfind(lines, ':');
if ~isempty(idxs)
name = lines(1:idxs(1)-1);
end
% // If we find "do you want" in this sentence, then the leader
% // is found, so quit.
if ~isempty(strfind(lower(lines), 'do you want'))
Leader_Name = name;
break;
end
end
By running the above code with your test case, this is what I get:
out2 = teamPowerHolder('teamPowerHolder_convo2.txt')
out2 =
Anna

Removing 'AllFiles' from file types when using uigetfile

I'm using uigetfile with a custom set of FilterSpecs. Here is the sentence:
[FileName,PathName,FilterIndex] = uigetfile({'*.wav';'*.mp3'},'Open Audio File');
As you can see my FilterSpec is {'*.wav';'*.mp3'} and this works perfectly fine. My problem is simple, is just that matlab is always appending AllFiles(*.*) to my FilterSpecs. I have searched in Matlab docs and it literally states:
"uigetfile appends All Files(.) to the file types when FilterSpec is a string.", but the problem is that I don't see another way of specifying a custom FilterSpec without using strings.Sorry if this results in a dumb question.
Thanks in advance
There's no way to (easily) remove the 'AllFiles' from uigetfile() since it's always added by MATLAB.
If you really want to do it, you have to copy the uigetputfile_helper() code (to
MYuigetputfile_helper() for example) and change it. And then you call it from your MYuigetfile() - same idea here.
The change would be around lines 311 and 319 in my version from uigetputfile_helper(), i.e.
% Now add 'All Files' appropriately.
if (addAllFiles)
% If a string, create a cell array and append '*.*'.
if (~iscell(returned_filter))
returned_filter = {returned_filter; '*.*'};
% If it is a cell array without descriptors, add '*.*'.
elseif (size(returned_filter, 2) == 1)
returned_filter{end+1} = '*.*';
end
end
Hope that helps... have fun!
If you back up a few lines from the previous poster's answer, you'll see a comment:
We want to add 'All Files' in all cases unless we have a cell array with descriptors.
This comment is on line 245 of uigetputfile_helper for me. Simply describe your file types at the time you call uigetfile, and you won't see All Files (*.*)
Example:
[fname,pname] = uigetfile({'*.m','MATLAB Code (*.m)';'*.mat','MATLAB Data (*.mat)'});